admin管理员组文章数量:1404923
I'm searching for a way to scroll down to a page that loads content as it is scrolled, to have everything loaded before I start interacting with it using Selenium.
I found this code below which was posted for c#, I changed it to Java. It piles and runs. But even though the page reaches the end, it does not get out of the loop
Boolean readyStateComplete = false;
while (!readyStateComplete)
{
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("window.scrollTo(0, document.body.offsetHeight)");
readyStateComplete = (String) executor.executeScript("return document.readyState") == "plete";
}
I don't know much about Javascript. How can this be corrected?
I'm searching for a way to scroll down to a page that loads content as it is scrolled, to have everything loaded before I start interacting with it using Selenium.
I found this code below which was posted for c#, I changed it to Java. It piles and runs. But even though the page reaches the end, it does not get out of the loop
Boolean readyStateComplete = false;
while (!readyStateComplete)
{
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("window.scrollTo(0, document.body.offsetHeight)");
readyStateComplete = (String) executor.executeScript("return document.readyState") == "plete";
}
I don't know much about Javascript. How can this be corrected?
Share Improve this question asked Apr 29, 2016 at 7:36 AryaArya 9,00532 gold badges114 silver badges187 bronze badges 2- 1 try .equals() method instead of == i means readyStateComplete = ((String) executor.executeScript("return document.readyState")).equals("plete"); – Ravi Kavaiya Commented Apr 29, 2016 at 7:43
- that did it. If you post it as an answer I'll accept it – Arya Commented Apr 29, 2016 at 7:51
2 Answers
Reset to default 2Please try .equals()
method instead of ==
i means
readyStateComplete = ((String) executor.executeScript("return document.readyState")).equals("plete");
== tests for reference equality (whether they are the same object).
.equals() tests for value equality (whether they are logically "equal").
A better way to scroll down the page and wait for the content to be loaded is to use executeAsyncScript
and then wait for the scroll height to change and for the state to be ready:
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.get("http://imgur./");
final String JS_SCROLL_DOWN =
"var callback = arguments[0], page = document.documentElement, height = page.scrollHeight; " +
"window.scrollTo(0, height); " +
"(function fn(){ " +
" if(page.scrollHeight != height && document.readyState == 'plete') " +
" return callback(); " +
" setTimeout(fn, 30); " +
"})();";
js.executeAsyncScript(JS_SCROLL_DOWN);
本文标签: javascriptusing documentreadyState correctly with SeleniumStack Overflow
版权声明:本文标题:javascript - using document.readyState correctly with Selenium - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744865745a2629334.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论