Javascript 如何使用Selenium正确等待列表滚动?
我编写了Selenium代码(Javascript),用键盘上的向下键自动向下滚动列表。我知道使用Javascript命令可以更轻松地滚动,但我想使用向下键 这是我目前的代码:Javascript 如何使用Selenium正确等待列表滚动?,javascript,node.js,selenium,asynchronous,async-await,Javascript,Node.js,Selenium,Asynchronous,Async Await,我编写了Selenium代码(Javascript),用键盘上的向下键自动向下滚动列表。我知道使用Javascript命令可以更轻松地滚动,但我想使用向下键 这是我目前的代码: let list = await this.driver.wait(until.elementLocated(By.css('#scrollableList')), 2000); // the list is not scrolled in the beginning: let scrolledY = aw
let list = await this.driver.wait(until.elementLocated(By.css('#scrollableList')), 2000);
// the list is not scrolled in the beginning:
let scrolledY = await this.driver.executeScript('return arguments[0].scrollTop;', list);
await assert.deepEqual(scrolledY, 0);
// clicking on the list and pressing the "Down"-Key multiple times to scroll down
await this.driver.actions({
bridge: true
}).click(list).perform();
await this.driver.actions({
bridge: true
}).keyDown(Key.DOWN).keyUp(Key.DOWN).keyDown(Key.DOWN).keyUp(Key.DOWN).keyDown(Key.DOWN).keyUp(Key.DOWN).perform();
// This is what I need so that the following assert return true:
await sleep(100);
// check if it is successfully scrolled down:
scrolledY = await this.driver.executeScript('return arguments[0].scrollTop;', list);
await assert.ok(scrolledY > 0);
我的问题是:
它成功地向下滚动(当我让程序休眠10秒时,我可以清楚地看到)。但是我需要排队等待睡眠(100)等待几毫秒。否则,最后一个断言返回它不起作用。
这是为什么?如果没有sleep命令,我如何使它工作?试试这段代码。您不需要添加任何内容。用这个代码替换你的等待睡眠
int timeOutInSeconds = 5;
ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
};
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(expectation);
int timeoutines=5;
ExpectedCondition expectation=新的ExpectedCondition(){
公共布尔应用(WebDriver驱动程序){
return((JavascriptExecutor)driver.executeScript(“return document.readyState”).equals(“complete”);
}
};
WebDriverWait wait=新的WebDriverWait(驱动程序,timeoutingseconds);
等待,直到(期望);
试试这段代码。您不需要添加任何内容。用这个代码替换你的等待睡眠
int timeOutInSeconds = 5;
ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
};
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(expectation);
int timeoutines=5;
ExpectedCondition expectation=新的ExpectedCondition(){
公共布尔应用(WebDriver驱动程序){
return((JavascriptExecutor)driver.executeScript(“return document.readyState”).equals(“complete”);
}
};
WebDriverWait wait=新的WebDriverWait(驱动程序,timeoutingseconds);
等待,直到(期望);
我猜JS命令是单独执行的,然后快速移动到assert。你能用这个.driver.executeAsyncScript尝试同样的脚本吗?我试过了。但是仅仅用executeAsyncScript
替换executesScript
是行不通的。代码不会结束并永远运行。我不知道为什么。我猜JS命令是单独执行的,然后快速移动到assert。你能用这个.driver.executeAsyncScript尝试同样的脚本吗?我试过了。但是仅仅用executeAsyncScript
替换executesScript
是行不通的。代码不会结束并永远运行。我不知道为什么。加载文档后,readyState属性始终为“完整”。如果我滚动某物,它不会改变,对吗?所以这行不通。。我希望在站点实际向下滚动后执行断言。加载文档后,readyState属性始终为“complete”。如果我滚动某物,它不会改变,对吗?所以这行不通。。我希望在站点实际向下滚动后执行断言。