I have an arbitrary number of tiles that have a unique class name. One of them has a unique title so I'm trying to grab that one specifically. Once I've done that, I want a reference to child element rather deep in its structure.
Does the "tile." part of tile.findElement() do anything for me here? I want to be specifically using an xpath relative to tile.
public WebElement getArticleTileByTitle(String title){
By xpath = By.xpath(".//div[contains(., '" + title + "') or @title='" + title + "']");
List<WebElement> tiles = findPresentElements(By.className("articleTile"));
for(WebElement tile : tiles){
if(isElementPresent(tile, xpath))
return tile;
}
return null;
}
public boolean articleTileImageIsVisible(WebElement tile){
WebElement background = tile.findElement(By.xpath("/*[2]/*[2]/*[1]/*[3]")); //This throws NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/*[2]/*[2]/*[1]/*[3]"}
//figure out stuff about background
}
I know that background is of type img so I've tried
tile.findElement(By.xpath("./img/*[2]/*[2]/*[1]/*[3]"));
tile.findElement(By.xpath("./*/*[2]/*[2]/*[1]/*[3]"));
tile.findElement(By.xpath(".//*/*[2]/*[2]/*[1]/*[3]"));
tile.findElement(By.xpath("./*/*[2]/*[2]/*[1]/*[3]"));
Never the less, I keep getting variations on NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/[2]/[2]/[1]/[3]"} and That's not a valid xpath.
This question is very similar to Selenium 2 - Can findElement(By.xpath) be scoped to a particular element? but people seem to have either misinterpreted it or my use case is different.
We can locate child nodes of web elements with Selenium webdriver. First of all we need to identify the parent element with help of any of the locators like id, class, name, xpath or css. Then we have to identify the children with the findElements(By. xpath()) method.
Go to the First name tab and right click >> Inspect. On inspecting the web element, it will show an input tag and attributes like class and id. Use the id and these attributes to construct XPath which, in turn, will locate the first name field.
Using ID as a Locator The best and the most popular method to identify web element is to use ID. The ID of each element is alleged to be unique. In this sample, we would access “Email” text box present in the login form at gmail.com.
The API documentation for findElements()
says:
When using xpath be aware that webdriver follows standard conventions: a search prefixed with "//" will search the entire document, not just the children of this current node. Use ".//" to limit your search to the children of this WebElement.
So you should be able to do something like:
WebElement foo = driver.findElement(By.xpath("//*[@title='" + title + "']"));
WebElement bar = foo.findElement(By.xpath(".//img"));
The leading dot in the second xpath makes all the difference, as it will search only for child elements of foo
.
Topic with similar question! - XPath to get all child nodes (elements, comments, and text) without parent
this part from that topic can interest you.
child::*
selects all element children of the context node
child::text()
selects all text node children of the context node
child::node()
selects all the children of the context node, whatever their node type
We can identify the child nodes from the parent, by localizing it with the parent and then passing ( ./child::*)
as a parameter to the findElements(By.xpath(""))
var childs = parent.findElements(By.xpath("./child::*"));
then in your code filter/process them...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With