Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search within an Selenium WebElement? [duplicate]

Tags:

java

selenium

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?

like image 246
user3561614 Avatar asked Jun 04 '16 13:06

user3561614


People also ask

How do you find an element within another element in Selenium?

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.

How do you search for text in WebElement?

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.

How do you get the count of similar objects list in the Web in Selenium?

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.


1 Answers

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
like image 90
Naman Avatar answered Sep 30 '22 06:09

Naman