how to identify all webelements in a weblist

29 views
Skip to first unread message

manoj kumar

unread,
Aug 2, 2016, 7:51:00 AM8/2/16
to webdriver
List<WebElement> links = driver.findElements(By.xpath(".//*[@id='sec1-right']/div[2]"));
List<WebElement> ele1=driver.findElements(By.xpath("//div[@class='all_sorts']"));
for(int i = 0;i<=links.size();i++)
{
System.out.println("Total number of links = " + links.size());
System.out.println(links.get(i).getText());
for(int j=0;j<=ele1.size(); j++)
{
System.out.println(ele1.get(i).getText());
}
}

darrell grainger

unread,
Aug 3, 2016, 4:09:51 PM8/3/16
to webdriver
Just a quick look at your code and I see that the first XPath (".//*[@id='sec1-right']/div[2]") should give you one element. You cannot have more than one element with the same id on a web page (or there is something wrong with the website you are testing). Then you are looking for the second div which is a child of that single element. There can only be one child in position 2. So the outer loop is pointless.

Rather than show us the code which does not work, you might want to talk about what you are trying to do, hopefully a link to the website you are testing AND the code that does not work.
Reply all
Reply to author
Forward
0 new messages