0

I run nearly 30-40 test cases twice a day. And every time some test case fails due to page load time. In fact I have explicitly mentioned Thread.sleep("2000"); wherever I feel it takes more time to load. But still some 5-6 testcases fails. How can I make my selenium to wait until the page loads ?? And I got this exception

Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 1828
              Only local connections are allowed.Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible(Session info: chrome=54.0.2840.99)
              (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)               Command duration or timeout: 36 milliseconds
              Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'System info: host: 'DESKTOP-LU4RI93', ip: '10.100.154.4', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111'
              Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\HRT1~1\AppData\Local\Temp\scoped_dir6436_32242}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=54.0.2840.99, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
              Session ID: dacb1d58627c657aa111d94eba60b81f
2
  • Are you using implicit wait? If yes, go for explicit waits. Nov 23, 2016 at 7:53
  • Maybe you are using the wrong wait, this is not a very good approach. blind wait (with no condition) should be avoided and a conditional/explicit wait should be used (wait seconds for some element or for the page to be loaded).
    – lauda
    Nov 24, 2016 at 9:03

4 Answers 4

1

You can find the answer in https://stackoverflow.com/questions/12858972/how-can-i-ask-the-selenium-webdriver-to-wait-for-few-seconds-in-java

in Short, you should use conditional wait.

2
  • ok i will use explicitwait thanks for support but Actually i a have an issue I cant click on Anchore tag the text between the tage i have my HTML like this <li class="item "> <a class="nav-link" href="/index.php/hrView/students">Students</a> </li> i want to click on student fail to click on it any suggestion ?/ Nov 23, 2016 at 8:17
  • @Ahmedali If you have new questions please use the 'Ask question' button and give enough context, like test code, html and what you researched. Your question is not related to this question Nov 23, 2016 at 8:27
1

You need to wait for an expected element to be shown on the page. This wait will loop until either the timeout is reached or the element is found.

Reads:

-1

I have faced this issue. Refer below working snippet -

static void waitForPageLoad(WebDriver driver) {
    WebDriverWait wait = new WebDriverWait(driver, 60);

    Predicate<WebDriver> pageLoaded = new Predicate<WebDriver>() {

        @Override
        public boolean apply(WebDriver input) {
            return ((JavascriptExecutor) input).executeScript("return document.readyState").equals("complete");
        }

    };
    wait.until(pageLoaded);
}
2
  • Maybe you can explain how the document.readyState will help? According to the documentation it is 'complete' when all resources have been loaded. Now still the front-end framework could be busy rendering and the needed element could still be hidden. Although it might work most of the time, this might still result into flaky tests I think. developer.mozilla.org/en-US/docs/Web/API/Document/readyState Nov 23, 2016 at 9:42
  • Suggested solution only waits for DOM readyState to signal complete. But Selenium by default tries to wait for those(& a little bit more) on page loads via the driver.get() & element.click() methods.They are already blocking,they wait for the page to fully load and those should be working ok Problem obviously,are redirects via AJAX requests & running scripts - those can't be caught by Selenium,it doesn't wait for them to finish.Also, you can't reliably catch them via readyState - it waits for a bit,which can be useful, but it will signal complete long before all the AJAX content is downloaded Nov 24, 2016 at 5:47
-3

I thin you should try and increase the wait time. You've added Thread.sleep("2000");, which is 2 seconds.

BUT

do all your pages load within 2 seconds?

I think you should try and add some more wait time.

1
  • 1
    Not a very good option :), blind wait should be avoided and a conditional wait should be used (wait seconds for some element or for the page to be loaded).
    – lauda
    Nov 24, 2016 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.