在下面的代码中,我希望看到大小为18,但它显示为0.我无法弄清楚原因。
我将Amazon搜索图书并最终想要将图书标题存储在一个数组中。谢谢!
@Test
public void searchTestOne(){
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.amazon.in");
driver.manage().window().maximize();
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Books");
driver.findElement(By.className("nav-input")).click();
List<WebElement> result = driver.findElements(By.xpath(".//*[@id='atfResults']/ul[@id='s-results-list-atf']/li"));
System.out.println(result.size());
答案 0 :(得分:0)
页面是动态的,这意味着一旦selenium认为页面已加载,内容实际上还没有在页面上。所以你必须先等待结果。
在这种情况下,您可以等到预期多个结果,因为列表应该一次加载:
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
//Note this isn't giving you the titles, it's giving you the entire list item
By bookSearchResults = By.xpath(".//*[@id='atfResults']/ul[@id='s-results-list-atf']/li");
wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(bookSearchResults, 1));
//Then continue on as you were
List<WebElement> result = driver.findElements(bookSearchResults);
....
您也可以尝试等待结果div。