0

I found a child by text in the following way.

Driver.FindElement(By.XPath("//span[contains(@class,'MyClass')][contains(text(),'MyText')]"));

The problem is that I need to click on the parent element. The element on the page appears dynamically and for earlier I only know the text. Is it possible to program this?

My html:

<div class="list-item list-item-station ui-draggable ui-draggable-handle">
    <div class="operations">
        <span class="fa fa-times"></span>
    </div>
    <div class="icon-image image-item">
        <span class="fa fa-user"></span>
    </div>
    <div class="icon-name editable-input-long">
        <span class="will-edit will-edit-input will-edit-textarea">MyText</span><textarea class="editable-  textarea"></textarea>
    </div>
</div>
Олег
  • 1
  • 1
  • hi, maybe this is helpful: https://stackoverflow.com/questions/28237694/xpath-get-parent-node-from-child-node – gries Oct 14 '19 at 15:20
  • Share HTML with parent element please – Sers Oct 14 '19 at 15:46
  • A user is created in the browser interface. Then he appears among others. It is necessary to choose it. I know the login under which it is created. Therefore, xpath is always different. I can contact directly by name as above. But you need to click on the parent element to open another page with its rights. – Олег Oct 14 '19 at 15:54
  • 1
    Possible duplicate of [XPath: Get parent node from child node](https://stackoverflow.com/questions/28237694/xpath-get-parent-node-from-child-node) – Greg Burghardt Oct 14 '19 at 17:16

4 Answers4

1

I'm using XPath:

    using (IWebDriver driver = new ChromeDriver(this.SeleniumDriverPath, driveroptions))
                {
                    try
                    {
                        driver.Navigate().GoToUrl("http://www.google.com/");

                        IWebElement query = driver.FindElement(By.Name("q"));
            query.SendKeys("link");
                    query.Submit();

                        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                        var listElement = driver.FindElements(OpenQA.Selenium.By.XPath(".//*[@id='search']//div[@class='g']"));
                        foreach( var e in listElement)
                        {
                                    // parent element:
                            var parent = e.FindElement(OpenQA.Selenium.By.XPath("./.."));   //parent element
    parent.Click();
                        }
                    }
                    catch(Exception ex)
                    {
                        ...
                    }

                    driver.Quit();
                }
yob
  • 462
  • 4
  • 7
0

With XPath you can do DOM navigation so you can start by grabbing the IWebElement that is the child and then use an XPath of .. to navigate up one level.

IWebElement child = Driver.FindElement(By.XPath("//span[contains(@class,'MyClass')][contains(text(),'MyText')]"));
child.FindElement(By.XPath("..")).Click();

If you are able to modify your child XPath, you can just find the parent in one lookup.

Driver.FindElement(By.XPath("//span[contains(@class,'MyClass')][contains(text(),'MyText')]/..")).Click();
                                                                                          ^ added the parent axis here

There are other ways to accomplish this but these two methods should get you started.

Reference:
MDN Web docs - parent

JeffC
  • 18,375
  • 5
  • 25
  • 47
0

You can get parent list-item div with xpath below:

Driver.FindElement(By.XPath("//span[contains(@class,'will-edit')][contains(text(),'MyText')]/ancestor::div[contains(@class,'list-item')][1]"));

To get list-item div with child span with MyText:

Driver.FindElement(By.XPath("//div[contains(@class,'list-item') and .//span[contains(@class,'will-edit')][contains(text(),'MyText')]]"));

Get textarea using span with MyText:

Driver.FindElement(By.XPath("//div[contains(@class,'list-item') and .//span[contains(@class,'will-edit')][contains(text(),'MyText')]]//textarea"));
Sers
  • 10,960
  • 2
  • 8
  • 25
0

JeffC's answer came up to me. Thank you very much. I redid the code a bit to fit my situation.

IWebElement child = Browser.FindElement(By.XPath("//span[contains(@class,'will-edit will-edit-input will-edit-textarea')][contains(text(),'"+name+"')]"));
child.FindElement(By.XPath("../..")).Click();  

I think other answers may come up. I did not try. Thank you all very much!

Олег
  • 1
  • 1