Alexander Rumanovsk
Alexander Rumanovsk

Reputation: 2303

Selenium not waiting for page to load

I'm doing some tests with Selenium, and the tests have to login to a system. This login takes 17 seconds to fully happen. The system has to wait for it to finish, otherwise the whole test fails.

I've tried many ways to do that, but all of them fail.

First code I've tried was this:

driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

When I use that, even though I tell it to wait for 100 seconds (which is almost 2 full minutes!), I get a timeout 2 seconds later with this stacktrace.

org.openqa.selenium.WebDriverException: timeouts
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'CMTCLX62137', ip: '53.19.227.206', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_31'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\ALEX\AppData\Local\Temp\rust_mozprofile.Z2KJE568nWB8, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, moz:headless=false, platform=ANY, proxy=Proxy(manual, http=localhost), specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=true, browserVersion=56.0, platformVersion=6.1, moz:processID=21116.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}]
Session ID: b2dca4a5-623a-4311-ad07-6444785dbcaf
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteTimeouts.implicitlyWait(RemoteWebDriver.java:868)

Another code I've tried:

new WebDriverWait(driver, 100).until(webDriver -> ((JavascriptExecutor) webDriver).executeScript(
        "return document.readyState").equals("complete"));

Using this, it just doesn't wait and I get a

org.openqa.selenium.NoSuchElementException: Unable to locate element

The only way for my test to work is using a Thread.sleep(), but this is a really bad option, because sometimes it loads faster than expected and sometimes it still fails because it takes more than 17s.

Any other option to wait for the page to fully load?

Upvotes: 2

Views: 8081

Answers (2)

iamsankalp89
iamsankalp89

Reputation: 4739

I guess used elementToBeClickable() with Explicit wait instead of Page Load

WebElement ele= driver.findElement("Locator Value");
WebDriverWait wait=new WebDriverWait(driver, 20); 
wait.until(ExpectedConditions.elementToBeClickable(ele));
ele.click();

Upvotes: 1

Igor Ávila
Igor Ávila

Reputation: 56

This is already solved here: Selenium wait until document is ready

Anyways I usually wait for controls I need to use, instead of waiting until full page is loaded:

wait.until(ExpectedConditions.elementToBeClickable(By
            .id(ConfigData.IDs.buttonLogin)));

Upvotes: 3

Related Questions