Module17: Webelement size | Selenium Forum
M
Posted on 26/09/2016
URL: https://uk.webuy.com/search/index.php?stext=*&section=&catid=956
[b:tpao88tw][u:tpao88tw]Scenario 1:[/u:tpao88tw][/b:tpao88tw]
List<WebElement> ab = d.findElements(By.xpath("//div[@class='searchRecord']/div[@class='desc']/h1/a"));
System.out.println(ab.size());
Size is displayed as [u:tpao88tw]15[/u:tpao88tw]
[attachment=0:tpao88tw]Webuy_list.png[/attachment:tpao88tw]

[b:tpao88tw][u:tpao88tw]Scenario 2:[/u:tpao88tw][/b:tpao88tw] when same xpath is used in different way, size is displayed as "[u:tpao88tw]1[/u:tpao88tw]"
WebElement a=d.findElement(By.xpath("//div[@class='searchRecord']/div[@class='desc']/h1"));
List<WebElement> ab=a.findElements(By.tagName("a"));
System.out.println(ab.size());
Size is displayed as "[u:tpao88tw]1[/u:tpao88tw]"
can you please let me know what is wrong in scenario 2 and why size is varying?
[attachment=1:tpao88tw]Webuy_Webelement.png[/attachment:tpao88tw]

M
Replied on 27/09/2016

use scenario 1.

there no need to use 2 lines which can be achieved in 1 line.


M
Replied on 27/09/2016

Can you please let me know what is wrong in scenario 2??

As results are varying in scenario2, can you please help me in identifying exact root cause of this issue?


M
Replied on 28/09/2016

you'll have to use for loop and do

for(int i=o;i<ab.length;i++)
a[i].findelements(by tagname (a));

this way you should get 15 results.