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 ffdfd
• 5,550 points
2,499 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 29, 2018 in Selenium by bug_seeker
• 15,520 points
1,693 views
0 votes
1 answer

How do you make Selenium 2.0 wait for the page to load?

You can also check pageloaded using following ...READ MORE

answered Nov 27, 2020 in Selenium by Gitika
• 65,910 points
419 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
1,475 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,460 points
7,179 views
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,500 points
12,714 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,608 views
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,910 points
6,683 views
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 3, 2020 in Selenium by Sri
• 3,190 points
7,551 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,910 points
9,584 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
13,479 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP