【发布时间】:2022-08-18 04:40:34
【问题描述】:
我使用这种方法从div 类中检索所有src。例如,在我的代码页面中有 5 个元素,但是当我运行此代码时,我只得到 2 个src。如果我多次运行我的代码,有时它会返回所有 5 个元素。
public static void main(String[] args) throws IOException, URISyntaxException {
System.setProperty(\"webdriver.chrome.driver\", \"S:\\\\behance-id\\\\src\\\\main\\\\resources\\\\chromedriver.exe\");
WebDriver driver = new ChromeDriver();
driver.get(\"https://www.behance.net/gallery/148589707/Hercules-and-Randy\");
List<WebElement> firstResult = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(\"//div[@class=\'ImageElement-root-kir ImageElement-loaded-icR\']/img\")));
for (WebElement webElement : firstResult) {
System.out.println(webElement.getAttribute(\"src\"));
}
driver.quit();
}
还尝试添加此行,但没有帮助:
((JavascriptExecutor)driver).executeScript(\"window.scrollTo(0, document.body.scrollHeight)\");
所以这个脚本不会返回所有需要的元素,即使它们是同一个类。
-
在您尝试获取元素时,元素可能尚未加载。尝试等待几秒钟
-
我将超时时间增加到 30 秒 - 它没有帮助。在这种情况下超时无效