Selenium在页面上显示的链接数为0,尽管页面上有很多链接。
这是我在java中的代码
dr.get("https://www.ebay.com");
List<WebElement> linksize = dr.findElements(By.tagName("a"));
System.out.println(linksize.size());
输出: 0
答案 0 :(得分:1)
等到页面加载链接
将代码修改为
dr.get("https://www.ebay.com");
waitForLoad(dr); // Here you are calling the below method
List<WebElement> linksize = dr.findElements(By.tagName("a"));
System.out.println(linksize.size());
您可以使用以下方法作为您的工具,并可以随时调用
void waitForLoad(WebDriver driver) {
ExpectedCondition<Boolean> pageLoadCondition = new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(pageLoadCondition);
}
答案 1 :(得分:0)
@Subhrapratim Bhattacharjee,似乎你需要等待页面加载。请尝试以下代码
WebDriver driver = new FirefoxDriver();
driver.get("https://www.ebay.com");
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 30);
List<WebElement> linksize = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName("a")));//driver.findElements(By.tagName("a"));
System.out.println(linksize.size());