Wait for page to load using HtmlUnitDriver

0 votes

I use HTML driver and when I run the tests and want to click on an element or read text, the elements can not be found. Every time a Exception is thrown I have to save the code of the Page.When I check the Code, the element is there and when I re run the test everything works fine. My guess is that the page was not completely loaded when I try to access the element. So I wait until Selenium has finished loading the page before I try to access elements.

I found two ways to achieve it:

  1. Execute Javascript (e.g. window.initComplete) and wait for the result to be true. 

  2. Wait for the last element on the page to load 

Any suggestions ?

May 7, 2018 in Selenium by Atul
• 5,530 points
428 views

1 answer to this question.

0 votes

You can create a custom ExpectedCondition

public static ExpectedCondition<Boolean> waitForLoad() {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("done");
        }
    };
}
answered May 7, 2018 by anonymous

Related Questions In Selenium

0 votes
1 answer

How to wait for the webpage to load in selenium using java

Firstly, when you launch a web application, ...READ MORE

answered Aug 28, 2018 in Selenium by bug_seeker
• 15,320 points
137 views
0 votes
1 answer

Wait for an element to be accessible using Selenium WebDriver

You can try out the below code: ...READ MORE

answered May 18, 2018 in Selenium by Atul
• 10,240 points
103 views
0 votes
1 answer

Selenium wait for Ajax content to load

you need to wait for Javascript and ...READ MORE

answered Jul 31, 2018 in Selenium by Meci Matt
• 9,400 points
1,828 views
0 votes
2 answers

How to set Page Load Timeout using C# using Selenium WebDriver

driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15); READ MORE

answered Apr 16 in Selenium by Tim Muir
1,384 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,050 points
405 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
761 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
401 views
0 votes
1 answer

How to eliminate this error”Cannot instantiate the type Select in selenium webdriver”

Try below code. Select sc = new Select(driver.findElement(By.xpath("your ...READ MORE

answered May 18, 2018 in Selenium by Samarpit
• 5,130 points
1,322 views
0 votes
1 answer

Fluent wait example with code

When we try to test the presence ...READ MORE

answered Jun 5, 2018 in Selenium by walter 123
• 240 points
6,908 views