2

Instead of using:

Sys.sleep(5)

to wait the dom to load is there any wait to wait until this command

document.readyState

give result "complete" so the dom is ready

Elen
  • 39
  • 7

1 Answers1

3

Selenium already implements polling of the document.readyState via the timeouts set of methods. RSelenium has the setTimeout methods. Increasing the timeout for page load should result in Selenium allowing more time for the operation to complete.

The problem comes when webpages don't finish loading. Some webpage are continuously loading calling themselves with delay. In this case you can check for the presence of an element in the DOM for example.

See

How I can check whether a page is loaded completely or not in web driver?

and

Selenium WebDriver : Wait for complex page with JavaScript(JS) to load

for further discussion. With RSelenium you can of course run JavaScript e.g.

remDr$executeScript("return document.readyState == 'complete';")

For modern webpages the issue of detecting when a page load is deemed to have completed can be problematic and solutions case specific.

Community
  • 1
  • 1
jdharrison
  • 30,085
  • 4
  • 77
  • 89
  • Thank you. Actually there are some ajax calls with I can't predict how long they take. If I use this `remDr$executeScript("return document.readyState == 'complete';")` will code wait until complete or I will have problem with exception? – Elen Sep 18 '16 at 11:08
  • If the AJAX calls expose data you could check for the existence of this data. There is an asynchronous version of the execute method. It will wait until its callback method is executed. You could use this together with appropriate JavaScript conditional on your case to wait until the call is complete. You would need to set the timeout on async scripts to an appropriate value also. – jdharrison Sep 18 '16 at 11:36
  • I tried to use `setTimeout(type = "page load", milliseconds = 10000)` and I have load the rselenium library but it mentions `Error: could not find function "setTimeout"` – Elen Sep 18 '16 at 11:51
  • Hopefully it is there https://github.com/ropensci/RSelenium/blob/master/R/remoteDriver.R#L329 . Are you calling it as a method of the remote driver class? For example `remDr$setTimeout(type = "page load", milliseconds = 10000)` – jdharrison Sep 18 '16 at 11:55
  • thank you. Yes it is. But unfortunately this doesn't work for all case. Selenium is not loading sometimes the whole page and give a general exception and my program stops. – Elen Sep 18 '16 at 14:39