简体   繁体   中英

How to wait page to load selenium java

I'm trying to find a way to make page to wait until all the elements in the page are fully updated. For example if I need to do a click which changes some value on my screen, and I want to wait until the value has been updated.

I'm searching for some generic action which I can use in many places in my program. The system is written with react, may I have any way to get to the reactjs components through java selenium? Or some other good way to make this wait happen?

you can use javascript executor to check entire web page has loaded. You need to add wait for 'document.readyState' to be equal to 'complete'.

This is a generic solution,for some of the elements you may need to add wait explicit wait. It depends on web application under test

To check if page has loaded, use code below

WebDriverWait wait = new WebDriverWait(driver,timeOutInSeconds);
ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
    @NullableDecl
    @Override
    public Boolean apply(@NullableDecl WebDriver input) {
        return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
    }
 };
wait.until(pageLoadCondition);

There is no silver bullet for waiting until the whole page has loaded with javascript frameworks like jQuery, react, AngularJS etc.

You will either have to wait until the last change in the page DOM has completed or the more proper way of waiting until the element you want to use is ready for consumption.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM