gpt4 book ai didi

java - Selenium Webdriver 无法使用标记名识别页面上的链接

转载 作者:行者123 更新时间:2023-12-01 09:26:54 26 4
gpt4 key购买 nike

尽管页面上有很多链接,Selenium 将页面上的链接数量显示为 0。

这是我的java代码

dr.get("https://www.ebay.com");
List<WebElement> linksize = dr.findElements(By.tagName("a"));
System.out.println(linksize.size());

输出:0

最佳答案

等待页面加载链接

将代码修改为

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());

您可以使用以下方法作为您的util,并且可以随时调用

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);
}

关于java - Selenium Webdriver 无法使用标记名识别页面上的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39760392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com