Selenium webdriver new tab URL is not loaded properly

China☆狼群 提交于 2021-01-28 21:50:50

问题


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.


回答1:


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;");
}


来源:https://stackoverflow.com/questions/53697301/selenium-webdriver-new-tab-url-is-not-loaded-properly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!