我只是尝试在搜索框中输入一个字符串,例如“ testing”,然后等待搜索列表可见,然后打印可见列表的计数。但计数为0。下面是我正在使用的代码。
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[@class=\"gLFyf gsfi\"]")).sendKeys("testing");
WebElement wait = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.className("UUbT9")));
List<WebElement> search_List=driver.findElements(By.xpath("//*[@role=\'listbox\']/li/div//span"));
System.out.println(search_List.size());
for(WebElement we:search_List) {
System.out.println(we.getText());
if(we.getText().contains("testing types")) {
we.click();
break;
}
}
您可以尝试使用此cssSelector
ul[class='erkvQe'] > li
编辑:您的代码在我端工作正常。我得到10。如果您仍然得到0。请尝试使用我的cssSelector。两者都可以。