Java 在使用SeleniumWeb驱动程序执行脚本之前,如何等待屏幕调整大小?
我目前在代码的这一部分有一个问题:Java 在使用SeleniumWeb驱动程序执行脚本之前,如何等待屏幕调整大小?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我目前在代码的这一部分有一个问题: webDriver.manage().window().setSize(new Dimension(width,height)); TimeUnit.MILLISECONDS.sleep(100); webDriver.executeScript("window.scrollTo(0,document.body.scrollHeight);"); 问题是,我希望在脚本执行和执行滚动之前调整屏幕大小,而sleep命令可以帮助我实现这一点。但是如果我删除它,滚动
webDriver.manage().window().setSize(new Dimension(width,height));
TimeUnit.MILLISECONDS.sleep(100);
webDriver.executeScript("window.scrollTo(0,document.body.scrollHeight);");
问题是,我希望在脚本执行和执行滚动之前调整屏幕大小,而sleep命令可以帮助我实现这一点。但是如果我删除它,滚动有时会在屏幕调整大小之前发生。我试着用一个预期状态检查来代替睡眠:
private boolean waitForScreenToResize(final int HEIGHT) {
WebDriverWait wait = new WebDriverWait(webDriver, 1);
ExpectedCondition<Boolean> screenResized = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver webDriver) {
return webDriver.manage().window().getSize().getHeight() == HEIGHT;
}
};
return wait.until(screenResized);
}
webDriver.manage().window().setSize(new Dimension(width,height));
waitForScreenToResize(height);
webDriver.executeScript("window.scrollTo(0,document.body.scrollHeight);");
private boolean waitForScreenToResize(最终整数高度){
WebDriverWait wait=新的WebDriverWait(webDriver,1);
ExpectedCondition screenResized=新的ExpectedCondition(){
@凌驾
公共布尔应用(WebDriver WebDriver){
返回webDriver.manage().window().getSize().getHeight()==HEIGHT;
}
};
返回wait.until(屏幕大小调整);
}
webDriver.manage().window().setSize(新尺寸(宽度、高度));
waitForScreenToResize(高度);
executeScript(“window.scrollTo(0,document.body.scrollHeight);”;
我原以为这会奏效,但问题仍然存在。有人有什么想法吗
编辑:忘了提到我想删除睡眠并用逻辑方法替换它,以检查屏幕是否已调整大小。我认为您指定的超时值太低
WebDriverWait wait = new WebDriverWait(webDriver, 1);
保存如下:
WebDriverWait wait = new WebDriverWait(webDriver, 10);
这允许该方法在引发异常之前,最多等待10秒以检查条件
从Selenium文档:
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
在抛出TimeoutException或
它发现元素将在0-10秒内返回它。WebDriverWait
默认情况下,每隔500毫秒调用ExpectedCondition,直到
成功返回。成功返回的值
ExpectedCondition函数类型是布尔值true或
非空对象
参考资料:
如果调整窗口大小时文档大小发生变化,您可以尝试等待
滚动高度发生变化:
String JS_GET_SCROLL_HEIGHT = "return Math.max(document.documentElement.scrollHeight, document.body.scrollHeight) | 0;";
String JS_SCROLL_BOTTOM = "window.scroll(0, -1 >>> 1);";
// get the current scroll height
Object scrollHeight = driver.executeScript(JS_GET_SCROLL_HEIGHT);
// set the size of the window
driver.manage().window().setSize(new Dimension(width, height));
// wait for the scroll height to change
new WebDriverWait(driver, 10)
.until(drv -> !drv.executeScript(JS_GET_SCROLL_HEIGHT).equals(scrollHeight));
// scroll to the bottom
driver.executeScript(JS_SCROLL_BOTTOM);
如果没有,您也可以尝试等待innerHeight
更改:
String JS_GET_INNER_HEIGHT = "return window.innerHeight | 0;";
String JS_SCROLL_BOTTOM = "window.scroll(0, -1 >>> 1);";
// get the current scroll height
Object innerHeight = driver.executeScript(JS_GET_INNER_HEIGHT);
// set the size of the window
driver.manage().window().setSize(new Dimension(width, height));
// wait for the scroll height to change
new WebDriverWait(driver, 10)
.until(drv -> !drv.executeScript(JS_GET_INNER_HEIGHT).equals(innerHeight));
// scroll to the bottom
driver.executeScript(JS_SCROLL_BOTTOM);
在新的WebDriverWait(webDriver,1)中将超时时间增加到5秒;试试看。我怀疑1秒钟是不够的。这并没有解决问题,我的问题是我想确保在滚动发生之前调整屏幕的大小。增加超时只会增加完成调整大小所允许的时间量,并且我没有收到任何TimeOutException,因此我认为这不是问题所在。谢谢你的意见。到底发生了什么?浏览器已调整大小,但条件未返回“True”?或者在调整浏览器大小之前条件返回“True”?请澄清条件返回true,这是正确的。但我认为,通过添加条件检查,可以确保在调整大小后发生滚动。有时情况并非如此,滚动是在调整大小之前发生的。这意味着在调整大小之前,您传递的高度值与当前窗口高度值相匹配。请检查您正在传递的值。请参考链接,编写ExpectedCondition的不同方法谢谢您的回答,我将研究这些,并希望返回更新。问题实际上与我没有发布的代码的下一部分有关,但您的解决方案对于我认为的问题也是有意义的。谢谢