我正在爲一些網絡創建一個自動測試,我正在使用WebDriver和用Java編寫的代碼。我在這裏面臨兩個問題。列表<WebElement>給出空白結果
click()
方法不起作用。- 當我試圖讀取WebElement的列表顯示爲空白。但它顯示正確
size()
。
我的代碼:
System.setProperty("webdriver.gecko.driver", "D:\\back up\\Proj\\selenium\\Jars\\Drivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.americangolf.co.uk/golf-clubs/fairway-woods");
List<WebElement> allBrands = driver.findElements(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/ul/li/a"));
List<WebElement> allBrandsInShowAll = driver.findElements(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[2]/ul/li/a"));
System.out.println("Total No of Brands "+(allBrands.size() + allBrandsInShowAll.size()));
driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[1]/a")).click(); // This is not clicking on ShowAll Button
for(WebElement ele :allBrandsInShowAll) {
System.out.println("Name=>"+ele.getText());
}
for(WebElement ele :allBrands) {
System.out.println("Name=>"+ele.getText()); // Not printing the names of Brands
}
你有沒有驗證的XPath,你正在嘗試獲取元素? xpath可能是錯誤的。 –
你想要在** allBrandsInShowAll **中獲得什麼? –
Xpath是正確的。我驗證了它。請檢查此網址「http://www.americangolf.co.uk/golf-clubs/fairway-woods」allBrandsInShowAll應該保留[+ Show All]按鈕點擊後的值。 [li值在不同的div下] – bbk