6

Imagine that you click on an element using RSelenium on a page and would like to retrieve the results from the resulting page. How does one check to make sure that the resulting page has loaded?

I can insert Sys.sleep() in between processing the page and clicking the element, but this seems like a very ugly and slow way to do things.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex
  • 19,533
  • 37
  • 126
  • 195

3 Answers3

5

Set ImplicitWaitTimeout and then search for an element on the page. From ?remoteDriver

setImplicitWaitTimeout(milliseconds = 10000)

Set the amount of time the driver should wait when searching for elements. When searching for a single element, the driver will poll the page until an element is found or the timeout expires, whichever occurs first. When searching for multiple elements, the driver should poll the page until at least one element is found or the timeout expires, at which point it will return an empty list. If this method is never called, the driver will default to an implicit wait of 0ms.

Community
  • 1
  • 1
jdharrison
  • 30,085
  • 4
  • 77
  • 89
  • 1
    that seems like a suboptimal solution: imagine that upon clicking a button you may get 2 different pages. one page that says here are your search results. another page that says sorry we have no search results. if you are trying to differentiate the two in your code using elements in the resulting page then this solution won't help right? since both calls to search for elements on the resulting pages will just error out – Alex Nov 22 '14 at 18:38
  • maybe i'm misunderstanding what you are saying? – Alex Nov 22 '14 at 18:38
  • It is a complicated problem which appears simple. http://www.obeythetestinggoat.com/how-to-get-selenium-to-wait-for-page-load-after-a-click.html gives some more background. Generally it will depend on your specific case. – jdharrison Nov 22 '14 at 18:39
  • yeh just read through that, that's the solution i'm using now but i was hoping there is something better – Alex Nov 22 '14 at 18:45
  • As Simon Stewart commented "Effectively, determining whether a page is fully loaded is a variation of the halting problem, which is non trivial to solve." https://groups.google.com/forum/#!msg/webdriver/7K2QWGVNCYo/2dfvZjer2UgJ – jdharrison Nov 22 '14 at 18:48
  • Am I correct in understanding that Selenium doesn't know when a click leads to a page but it *does* know when a page has finished loading as a result of the `navigate()` function? – Alex Nov 24 '14 at 19:43
2

In the RSelenium reference manual, you will find the method setTimeout() for the remoteDriver class:

setTimeout(type = "page load", milliseconds = 10000)

Configure the amount of time that a particular type of operation can execute for before they are aborted and a |Timeout| error is returned to the client.

type: The type of operation to set the timeout for. Valid values are: "script" for script timeouts, "implicit" for modifying the implicit wait timeout and "page load" for setting a page load timeout. Defaults to "page load"

milliseconds: The amount of time, in milliseconds, that time-limited commands are permitted to run. Defaults to 10000 milliseconds.

This seems to suggests that remDr$setTimeout() after remDr$navigate("...") would actually wait for the page to load, or return a timeout error after 10 seconds.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam Gong
  • 29
  • 4
1

You can also try out this code that waits for the browser to provide whether the page loaded or not.

objExecutor = (JavascriptExecutor) objDriver;
if (!objExecutor.executeScript("return document.readyState").toString()
    .equalsIgnoreCase("complete")){
    Thread.sleep(1000);
}

You can simply put it in your base page, so you won’t need to write it down in every pageobject. I have never tried it out with any Ajax enabled sites, but this might help you and your scenario dependency will also get away.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vivek Singh
  • 3,641
  • 2
  • 22
  • 27