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,540 points

1 answer to this question.

0 votes

You can create a custom ExpectedCondition

public static ExpectedCondition<Boolean> waitForLoad() {
    return new ExpectedCondition<Boolean>() {
        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,510 points
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
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,460 points
0 votes
3 answers

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

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

answered Apr 16, 2019 in Selenium by Tim Muir
0 votes
2 answers

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,520 points
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
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,890 points
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
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,890 points
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