Selenium - locating multiple elements with the same class name

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Selenium - locating multiple elements with the same class name



Hello there I am trying to locate multiple elements with the same className. The className and the body structure of the elements are the same but the texts, links and pictures are different.


<div class="dc-content-right clearfix"> (parent)
<div class="dc-item clearfix"> (child nodes)
<div class="dc-item clearfix">
<div class="dc-item clearfix">



Each of these child elements looks something like this:


<div class="dc-item clearfix">
<div class="dc-icon">
<div class="dc-info">
<h2>
<a href="http://www.avg.com/ww-en/free-antivirus-download">AVG AntiVirusFree 2015</a>
</h2>



Each child element has different text in the H2 tag. So once it is AVG AntiVirus Free 2015, then it is Internet Security .... and so on. So what I want to do is save all elements into a list and then work with them.
At first I save these elements intto a list of WebElements :


List <"WebElement"> list = driver.findElements(By.xpath("//div[@class='dc-item clearfix']"));



Afterwards I want to iterate through the list and write the h2 text for each element on the screen:


for(WebElement i:superDiv)

System.out.println(i.findElement(By.xpath("//h2/a")).getText());



So the outcome should be a list of 3 different headings extracted from divs. The problem: the outcome is the list of 3 headings which are the same!


divs


AVG AntiVirus Free 2015
AVG AntiVirus Free 2015
AVG AntiVirus Free 2015



It looks like I located the same element 3 times. Does anyone have an idea what could be the problem? Thank you




3 Answers
3


List<WebElement> list = driver.findElements(By.xpath(".//*[@class='dc-item clearfix']//h2/a"));
for(WebElement el : list)
System.out.println(el.getText());





Thank you, that will write all the text it can found in a child element, (links, descriptions, header...) I would like to access the h2 tag only.
– Zawe
Jul 15 '15 at 8:57





ya, i misread the question. I have updated the xpath since, does the new one work?
– Cathal
Jul 15 '15 at 8:58





Yes it does work. Thank you. But the whole point was.. is it possible to first save the whole child element. List <"WebElement"> list = driver.findElements(By.xpath("//div[@class='dc-item clearfix']")); and then acces the differnt parts of it. Something like list.get(1).findElement(By.xpath("//h2/a")).getText(). Do you know wheter something like this is even possible ?
– Zawe
Jul 15 '15 at 9:43






ah ok, gotcha. I dont know, but i would assume it is possible. trial and error is the only way you'll find out.
– Cathal
Jul 15 '15 at 10:12





That is also possible. You have to give xpath from child tag of that div element.
– Saritha G
Jul 15 '15 at 10:33



You can also try:-


List<WebElement> list = driver.findElements(By.xpath(".//*[@class='dc-info']//a"));
for(WebElement element : list)
System.out.println(element.getText());



If div tag does not contains id or class, then use like this


List<WebElement> elements = driver.findElements(By.xpath("//div[contains(@style,'height:50px')]"));
for(WebElement element : elements)
System.out.println(element.getText());






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered