2012-07-18 58 views
0

我一直在測試自動化,並且我是用facebook搜索並希望獲得數組列表中的鏈接。因此,在我的程序中,我打開www.google.com使用Firefox Web驅動程序,然後在搜索欄中輸入Facebook,然後嘗試掃描頁面。我能打開瀏覽器和搜索領域 與Facebook的頁面,但是當我使用List<String>linksWOSpace=Arrays.asList(selenium.getAttribute("//li/div/h3/a"));Xpath not found wen using selenium

它拋出我的錯誤字符串索引越界-1 它基本上不能由XPath來獲取元素

+0

['getAttribute()'](http://selenium.googlecode.com/svn/trunk/docs/api/java/com/thoughtworks/selenium/Selenium.html#getAttribute%28java.lang.String% 29)方法返回一個'String'。另外,它的用法是'「locator @ attribute」'。你能在這裏張貼你的預期產出嗎? – 2012-07-18 23:17:55

+0

所以在linksWOSpace我想[「歡迎來到Facebook - 登錄,註冊或瞭解更多」,「Facebook應用 - Google Play」,「Facebook」,「Facebook - 維基百科,自由的百科全書」,,,,]和等等 – karan 2012-07-18 23:28:53

回答

0

我不認爲你想要的是與Selenium RC一起實現的。你可以這樣做:

List<String> linksWOSpace = new ArrayList<String>(); 

int numberOfLinks = selenium.getXpathCount("//li/div/h3/a"); 
for (int i = 1; i <= numberOfLinks; i++) { 
    // gets the <a> elements one by one 
    linksWOSpace.add(selenium.getText("xpath=(//li/div/h3/a)[" + i + "]")); 
} 

輸出應該是所有的錨文本的List