Selenium switch要返回错误org.openqa.selenium.WebDriverException:未知错误:无法确定加载状态

J. *_*oem 5 java selenium webdriver selenium-webdriver

我有单击超链接的方案,并将打开新选项卡(单击超链接后活动窗口将移至新选项卡)

当我尝试使用switchTo()方法将WebDriver移至新选项卡时,随后WebDriverWait.until浏览器自动关闭并出现错误

org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status
from no such execution context
  (Session info: chrome=73.0.3683.103)
Run Code Online (Sandbox Code Playgroud)

我使用,System.out.println(driver.getWindowHandle())并且可以看到驱动程序移至新选项卡。

我如何解决以上错误?我尝试使用Iterator循环进入windowHandle

似乎无法使用WebDriverWait.until(ExpectedConditions)等待新选项卡。总是出错cannot determine loading status from no such execution context

奇怪的事情,我可以使用Thread.sleep(1000)

在这种情况下,如何避免使用Thread.sleep?因为隐式等待也无法工作

使用Thread.sleep()的工作代码

public class MyCode {
    private WebDriver driver;
    private WebDriverWait wait;

    @Test
    public void openPrestaShopFromDemoWebsite() {
        System.setProperty("webdriver.chrome.driver", "chromedriver");
        ChromeOptions chromeOptions = new ChromeOptions()
                .addArguments("--start-maximized", "--incognito");

        driver = new ChromeDriver(chromeOptions);
        driver.navigate().to("http://demo.prestashop.com");
        wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loadingMessage")));

        driver.switchTo().frame("framelive");
        String parentTab = driver.getWindowHandle();
        driver.findElement(By.partialLinkText("Ecommerce software by PrestaShop")).click();

        Set<String> windowHandles = driver.getWindowHandles();
        Iterator<String> it = windowHandles.iterator();

        while (it.hasNext()) {
            String newTab = it.next();

            if (!parentTab.equals(newTab)) {
                driver.switchTo().window(newTab);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                wait.until(ExpectedConditions.titleIs("Create and develop your business with PrestaShop"));
                driver.close();
            }
        }

        driver.switchTo().window(parentTab);
        driver.switchTo().frame("framelive");
        assertThat(driver.findElement(By.linkText("Personal info")).isDisplayed());

        driver.quit();

    }
}
Run Code Online (Sandbox Code Playgroud)

无法正常工作的代码(无法从此类执行上下文确定加载状态)

public class MyCode {
    private WebDriver driver;
    private WebDriverWait wait;
    private WebElement element;

    @Test
    public void openPrestaShopFromDemoWebsite() {
        System.setProperty("webdriver.chrome.driver", "chromedriver");
        ChromeOptions chromeOptions = new ChromeOptions()
                .addArguments("--start-maximized", "--incognito");

        driver = new ChromeDriver(chromeOptions);
        driver.navigate().to("http://demo.prestashop.com");
        wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loadingMessage")));

        driver.switchTo().frame("framelive");
        String parentTab = driver.getWindowHandle();
        driver.findElement(By.partialLinkText("Ecommerce software by PrestaShop")).click();

        Set<String> windowHandles = driver.getWindowHandles();
        Iterator<String> it = windowHandles.iterator();

        while (it.hasNext()) {
            String newTab = it.next();

            if (!parentTab.equals(newTab)) {
                driver.switchTo().window(newTab);

                wait.until(ExpectedConditions.titleIs("Create and develop your business with PrestaShop"));
                driver.close();
            }
        }

        driver.switchTo().window(parentTab);
        driver.switchTo().frame("framelive");
        assertThat(driver.findElement(By.linkText("Personal info")).isDisplayed());

        driver.quit();

    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

我已经遇到同样的问题几天了,看来这实际上是 chromedriver 的问题。对我有用的解决方案是将以下标志/参数添加到您的 chromedriver 选项中:

--disable-site-isolation-trials
Run Code Online (Sandbox Code Playgroud)

是我指的 chromedriver 问题(和解决方案)。

如果该标志不起作用,您也可以尝试升级到 chromedriver v75


Sam*_*ora -2

由于链接在新选项卡中打开,您需要将驱动程序切换到新选项卡,为此,您可以使用以下代码:

ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
Run Code Online (Sandbox Code Playgroud)

而如果你想切换到原来的选项卡,则需要使用:

driver.switchTo().window(tabs.get(0));
Run Code Online (Sandbox Code Playgroud)