在 selenium 中向下滚动,同时加载更多数据 - 延迟加载
scrolling down in selenium while more Data loads- Lazy loading
您好,我使用以下代码编写了向下滚动页面的代码:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
但是,它只滚动一次,而更多数据会加载到我的页面上。数据包含在 div 标签内。
向下滚动时,页面显示正在加载并加载更多数据(延迟加载)。
我如何在其中实现延迟加载的滚动?
此处示例如何滚动直到数据元素的大小不变。要打开 loadingLocator,请打开 chrome devtools,滚动并按 F8,它将暂停,您将能够获取选择器。
WebDriverWait wait = new WebDriverWait(driver, 5);
JavascriptExecutor js = (JavascriptExecutor) driver;
int dataSize = driver.findElements(dataLocator).size();
while (true){
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
wait.ignoring(NoSuchElementException.class)
.until(ExpectedConditions.invisibilityOfElementLocated(loadingLocator));
if (driver.findElements(dataLocator).size()== dataSize)
break;
dataSize = driver.findElements(dataLocator).size();
}
您也可以像下面这样实现自己的等待:
By dataLocator = By.cssSelector(".save_ride_container");
WebDriverWait wait = new WebDriverWait(driver, 5, 500);
JavascriptExecutor js = (JavascriptExecutor) driver;
AtomicInteger dataSize = new AtomicInteger(driver.findElements(dataLocator).size());
boolean complete = false;
while (!complete){
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
complete = wait.ignoring(TimeoutException.class)
.until(d -> {
int size = driver.findElements(dataLocator).size();
if (size== dataSize.get())
return true;
dataSize.set(size);
return false;
});
}
List<WebElement> passengers = driver.findElements(dataLocator);
passengers.forEach(p -> System.out.println(p.findElement(By.xpath("./div[1]/div[3]/p[5]/strong"))));
您好,我使用以下代码编写了向下滚动页面的代码:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
但是,它只滚动一次,而更多数据会加载到我的页面上。数据包含在 div 标签内。
向下滚动时,页面显示正在加载并加载更多数据(延迟加载)。 我如何在其中实现延迟加载的滚动?
此处示例如何滚动直到数据元素的大小不变。要打开 loadingLocator,请打开 chrome devtools,滚动并按 F8,它将暂停,您将能够获取选择器。
WebDriverWait wait = new WebDriverWait(driver, 5);
JavascriptExecutor js = (JavascriptExecutor) driver;
int dataSize = driver.findElements(dataLocator).size();
while (true){
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
wait.ignoring(NoSuchElementException.class)
.until(ExpectedConditions.invisibilityOfElementLocated(loadingLocator));
if (driver.findElements(dataLocator).size()== dataSize)
break;
dataSize = driver.findElements(dataLocator).size();
}
您也可以像下面这样实现自己的等待:
By dataLocator = By.cssSelector(".save_ride_container");
WebDriverWait wait = new WebDriverWait(driver, 5, 500);
JavascriptExecutor js = (JavascriptExecutor) driver;
AtomicInteger dataSize = new AtomicInteger(driver.findElements(dataLocator).size());
boolean complete = false;
while (!complete){
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
complete = wait.ignoring(TimeoutException.class)
.until(d -> {
int size = driver.findElements(dataLocator).size();
if (size== dataSize.get())
return true;
dataSize.set(size);
return false;
});
}
List<WebElement> passengers = driver.findElements(dataLocator);
passengers.forEach(p -> System.out.println(p.findElement(By.xpath("./div[1]/div[3]/p[5]/strong"))));