Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using document.readyState correctly with Selenium

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 compiles 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") == "complete";
        }

I don't know much about Javascript. How can this be corrected?

like image 928
Arya Avatar asked Feb 11 '26 15:02

Arya


1 Answers

Please try .equals() method instead of == i means

readyStateComplete = ((String) executor.executeScript("return document.readyState")).equals("complete"); 

== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they are logically "equal").

like image 76
Ravi Kavaiya Avatar answered Feb 13 '26 09:02

Ravi Kavaiya