0

The page I am testing, it's loading forever due to the slow network(or Internet censorship, some of the resources are bound to fail loading). However most of elements of the page are present within 1 second.

Since Selenium won't have an element clicked until the page's fully loaded, I want to use pageLoadTimeout() to stop the page from loading after 5 seconds and then handle the timeout and do something on the page. And the code works for the page which I specified in get(url).

    try {
        driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
        driver.get(url);
    }
    catch (TimeoutException e){
    }
    finally {
        driver.findElement(By.xpath("xpath here")).click();
    }

However this won't work if you're trying to click a link and then be navigated to a new page. You're will soon get

org.openqa.selenium.TimeoutException

because the code above didn't handle this new page's timeout exception. Then I'll have to add another try-catch-finally to prevent this new page from infinitely loading. This will get trickier if there're a lot of links you have to go thru before you are actually reaching the page you wanna test.

Does anyone has a solution to this?

Jacob
  • 51
  • 1
  • 8
  • Possible duplicate of [pageLoadTimeout in Selenium not working](https://stackoverflow.com/questions/45591282/pageloadtimeout-in-selenium-not-working) – undetected Selenium Nov 24 '17 at 09:31
  • I fail to understand the logic behind why to try to catch the exception in the first place when `PageLoading` fails? – undetected Selenium Nov 24 '17 at 09:33
  • Hi @DebanjanB , because I am using pageLoadTimeout() to stop the page from loading after 5 seconds. – Jacob Nov 25 '17 at 08:46
  • But how would you `findElement` if `PageLoading` fails? In the `catch` or `finally` you can only call `driver.quit()` – undetected Selenium Nov 25 '17 at 08:59
  • The page it's loading forever due to the slow network, however most of elements of the page are present within 1 second. Thus I want to use pageLoadTimeout() to stop the page from loading after 5 seconds and then handle the timeout and click the link. Otherwise if the page is loading non-stop, then selenium will never click the link. – Jacob Nov 25 '17 at 09:04
  • @DebanjanB When pageLoadTimeout() gets a timeout, it will remain at where it is. So then I can have selenium locate the element and click on it. So my code works at the first page. But it fails at the second page because I didn't handle the second timeout. – Jacob Nov 25 '17 at 09:07

2 Answers2

0

Once pageLoadTimeout is added in the script, the WebDriver instance waits for 5 seconds for every page to get loaded before throwing an exception. If the page is not loaded in 5 seconds of time, then it throws TimeOutException at run time.

You should try increasing the timeout.

Vineet Bhat
  • 148
  • 2
  • 12
  • Thank you. But my intension is to use pageLoadTimeout() to stop the page from loading forever. I have updated the beginning of description to elaborate my situation here. – Jacob Nov 25 '17 at 09:23
  • Based on your description I understand that you don't want to wait for the entire page to load as the element that you were waiting for on the page has already loaded. So you should use explicitWait here to just wait for the element and perform your intended action over it. Same goes for the next page which is throwing the timeout error. Just wait for the element you are interested in. – Vineet Bhat Nov 26 '17 at 10:32
  • I did try explicitWait() but it won't work. The code will be stuck at driver.get(url) because the page is not fully loaded. Codes after that won't be executed. – Jacob Nov 26 '17 at 14:10
  • You can give this a shot. The temporary workaround is... messing with Firefox's timeout settings. Basically by default Firefox waits about 250 seconds for each connection before timing you out. You can check about:config for the details. Crank it down so Firefox doesn't wait too long and Selenium can continue as if the page has already finished loading. Source: [https://stackoverflow.com/questions/21214340/make-selenium-webdriver-stop-loading-the-page-if-the-desired-element-is-already] – Vineet Bhat Nov 26 '17 at 14:31
  • Thanks a bunch. I'll give it a try on Chrome. – Jacob Nov 26 '17 at 14:39
  • Unfortunately chrome doesn't seem to support this kind of configuration. https://superuser.com/questions/633648/how-can-i-change-the-default-website-connection-timeout-in-chrome – Jacob Nov 26 '17 at 15:19
  • Take a look at the approaches discussed in [Source 1](https://sqa.stackexchange.com/a/6355) and [Source 2](https://stackoverflow.com/a/13749867/330325). – Vineet Bhat Nov 27 '17 at 08:32
  • I am not following Source 2. It says "This code has to be put where a TimeOut is detected". Does that mean I should put it in a "catch" section? – Jacob Nov 27 '17 at 09:55
  • I have never used WebDriverBackedSelenium. But if you keep your above code as it is, then it seems that you will have to put it in finally as your timeout happens when you click and request for a new page. Otherwise, the first timeout is happening at the .get() statement and the second at the .click() statement. Also, if you just need to navigate through two pages, you could try using nested try-catch and wrap the .click() statement within try and catch the timeout exception. – Vineet Bhat Nov 28 '17 at 04:23
  • Thanks man.The thing is I have to navigate through more than 2 pages.. I might as well give up...This issue is consuming so much of my time. I will probably write an answer to summarize methods I tried. Hope that I can come across a solution in the future. Anyways thanks again for digging this up. Sorry that I can't upvote you cause I don't have the reputation required. – Jacob Nov 28 '17 at 09:43
0

Your approach - stopping page loading after 5 seconds - probably will not work.
WebDriver intentionally waits for a full page load. Many ajaxifed webpages havily use onload event to fire some javascript code just after a whole page has been loaded. This javascript code usually does some actions that prepares the page for use.

If you do not wait for a full page load and you start clicking or typing some data on this page, it's very likely that your test will not work - some fields could not be yet editable, clicking on buttons or links may not work etc.

WebDriver tries to prevent such errors, and most of it's methods like clicking and writing data are waiting for the full page to load by default.

krokodilko
  • 35,300
  • 7
  • 55
  • 79
  • Thanks. But a full page load is not likely to happen because of the Internet censorship here. So usually 5 seconds is enough to load most of the page(except for those censored resources. They're the reason that the page can't be fully loaded but won't affect features I am testing on this page). And my code above can do the trick on the first page. I need to find a way for those links to new pages. – Jacob Nov 26 '17 at 14:25