简体   繁体   中英

Selenium webdriver new tab URL is not loaded properly

I am trying to open a new tab from tab1 (Now i have two different tab. Tab1 & Tab2), the second tab with the URL is not fully loaded, instead it stops at the middle and shows tab as untitled.

public void waitForPageLoadComplete(WebDriver driver, int specifiedTimeout) {
        Wait<WebDriver> wait = new WebDriverWait(driver, specifiedTimeout);
        wait.until(driverTemp -> String.valueOf(((JavascriptExecutor) driverTemp).executeScript("return document.readyState"))
                .equals("complete"));
    }

In the above code,i let the driver to wait for specific time to load the page properly or return timedout, but the above code is not working either, if i use Thead.sleep(millSec), the thread is waited for sometime, till that time the page is getting loaded without any error. can someone help me how to load the URL in the new tab without using thread.sleep.

First you have to switch driver to the tab where the javascript should be executed. I use this:

public void goto2() throws InterruptedException {
    ArrayList<String> winHandles = new ArrayList<String> (driver.getWindowHandles());
    // it takes some ms to get the WindowHandles, so this solution does not avoid Thread.sleep completely
    Thread.sleep(500);
    driver.switchTo().window(winHandles.get(1));
}

Another thing is the javascript itself. To wait for page is fully loaded I use this:

public void waitDom() {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("window.onload = null;");
}

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