Reputation: 1
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>
Upvotes: 0
Views: 1633
Reputation: 1
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!
Upvotes: 0
Reputation: 12255
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"));
Upvotes: 0
Reputation: 25597
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
Upvotes: 0
Reputation: 528
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();
}
Upvotes: 1