I'm generating a List of WebElements with:
List<WebElement> elements = driver.findElements(By.className("name"));
Now I'd like to get subelements from each element in elements
. I tried solutions like
WebElement element = elements.get(0).findElement(By.xpath("/x"));
where x is the tagname
of each element in elements. But this searches in the complete active frame and not only in the subelements of elements.get(0)
Any idea?
We can find an element in a sub-element with Selenium webdriver. First of all we need to identify the element with help of any of the locators like id, class, name, xpath or css. Then we have to identify the sub-element with the findElements(By. xpath()) method.
We can get text from a webelement with Selenium webdriver. The getText() methods obtains the innerText of an element. It fetches the text of an element which is visible along with its sub elements.
To get all the elements which has 'id', You can simply use contains in xpath and '*' to accept any element regardless of the type. List<WebElement> lst = driver. findElements(By. xpath("//*[contains(@id,'') ]")); //here '' is an empty char int size = lst.
Probably all that you need is change the code to :
WebElement element = elements.get(0).findElement(By.xpath(".//x")); //this would search any **x** as a sub-child of your list's elements
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