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
Add a ment  | 

2 Answers 2

Reset to default 2

Please 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