开发者

Need to print out all links on a sidebar in selenium (xpath?)

I need to find any extra links and print them out. I started by doing:

get_xpath_count(开发者_开发百科'//li/a')

and comparing it to the size of an array that holds the name of all the links for the sidebar. When the count is too high/low, I need to print out all the extra/missing links. I would like to make a list of the names so I can compare it to the array. I've tried a few things like get_text('//li/a'), which returns the name of the first. get_text('//li/a[1]) does the same, but any other index returns nothing.

Any ideas? Also, I need the name that's displayed on the link, not the actual href.

Edit* Also, i'm pretty new to selenium and Xpath. Please let me know if there's info I let out that is needed, or just any suggestions towards thew way I'm going about this.


I have been able to get this to work using CSS element locators. Since I use CSS selectors far more often than Xpath, I find it easier to always use them with Selenium as well.

$selenium->get_text("css=li a:nth-child(1)")
$selenium->get_text("css=li a:nth-child(2)")
$selenium->get_text("css=li a:nth-child(...)")
$selenium->get_text("css=li a:nth-child(n)")


Use:

(//li/a)[$someNumber]

this will get you the text of $someNumber-th //li/a in the XML document.

In order to know what values to use to substitute the $someNumber with, you need to know the total count of these elements:

count(//li/a)


This is in JAVA. You can use the same concept in perl

int totCountInPage=selenium.getXpathCount(//li/a);
for(int count=1;count<=totCountInPage;count++)
System.out.println(selenium.getText("xpath=//li[count]/a"));

This should print text inside the anchor links under all li tag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜