Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for page to fully load with webdriverjs

What's the best way to wait for a page to fully load using selenium-webdriver for javascript? I noticed this question is quite similar but I need an implementation in javascript.

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .build();

driver.get('http://www.google.com');

// Wait for the page to fully load here...
// Something like this...
// driver.wait(...);

// Then do other stuff here

driver.quit();
like image 597
JoshA Avatar asked Jun 01 '17 23:06

JoshA


People also ask

How will you wait until a Web page has been loaded completely?

We can wait until the page is completely loaded in Selenium webdriver by using the JavaScript Executor. Selenium can run JavaScript commands with the help of the executeScript method. We have to pass return document.

Does Selenium wait for page to load python?

For Python, you will have to implement Selenium Wait for page to load in order to ensure that tests are performed with the necessary WebElements in the DOM. Certain websites have some components or elements hidden, or not visible at an initial stage.

Which wait command will wait indefinitely for page load?

PageLoadTimeout Command This command establishes the time WebDriver must wait for a page to completely load before triggering an error. In case the timeout set is negative, the page load time can be indefinite.


1 Answers

I found that this works for what I needed.

driver.get('http://www.google.com');

driver.wait(function() {
  return driver.executeScript('return document.readyState').then(function(readyState) {
    return readyState === 'complete';
  });
});

// Do stuff after page load here
like image 176
JoshA Avatar answered Sep 28 '22 17:09

JoshA