【问题标题】:Exception (org.openqa.selenium.NoSuchWindowException): using Selenium WebDriver with Java in Internet Explorer异常 (org.openqa.selenium.NoSuchWindowException):在 Internet Explorer 中使用 Selenium WebDriver 和 Java
【发布时间】:2013-05-15 06:08:28
【问题描述】:

在使用 Selenium Webdriver 使用 IE 进行自动化时,我可以打开 URL,但在该页面上找到该元素会引发以下异常:

org.openqa.selenium.NoSuchWindowException:找不到元素 关闭窗口(警告:服务器未提供任何堆栈跟踪 信息)

我尝试了driver.switchTo.window() 方法,但它不起作用。 我已经搜索了几个小时,但我没有找到任何地方。

代码如下:

public static Selenium selenium;

public static void main(String args[]) {

    try {

        System.setProperty(
            "webdriver.ie.driver",
            "D:\\Driver\\IEDriverServer_Win32_2.32.3_latest\\IEDriverServer.exe");

        DesiredCapabilities capab = DesiredCapabilities.internetExplorer();
        capab.setCapability(
            InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
            true);

        WebDriver driver = new InternetExplorerDriver(capab);
        driver.get("http://www.google.com");
        driver.findElement(By.xpath(".//*[@id='addlang']/a[1]")).click();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • 请注意下面的答案,添加该功能确实会导致您的测试不稳定,在删除该设置之前,甚至没有必要诊断您的问题。我还恳请您不要使用 Google 进行测试(除非您有需要,而且我会对它感兴趣,因为我会押注您不这样做'不一定需要在 Google 的 UI 中进行实际搜索,有一些方法可以绕过它)。这是一个非常复杂的页面。请使用更简单的页面。
  • 感谢阿兰的回复。我也尝试使用谷歌以外的页面,但得到了同样的错误。但是,我想我已经理解了这个问题。它与设置 INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS 能力有关。再次感谢..
  • 我在使用 IE 11 时遇到了同样的异常。我没有使用任何功能,但不幸的是我遇到了该异常。
  • driver.switchTo.window() 不是解决这个问题的完美代码。

标签: java selenium internet-explorer exception selenium-webdriver


【解决方案1】:

删除功能INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS 并手动将您的 IE 保护模式设置设置为对所有区域都相同。

来源:

  1. http://jimevansmusic.blogspot.com/2012/08/youre-doing-it-wrong-protected-mode-and.html

  2. NoSuchElementException is occurred during implementation of InternetExplorerDriver in Selenium WebDriver

【讨论】:

  • 感谢您的回复。是的,我也尝试手动设置,但我在办公环境中工作,我没有管理员权限,所以我需要设置 INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,否则它将无法正常工作。还有其他方法吗?
  • 您应该询问您的办公室管理员,您需要进行更改以使您的自动化测试正常工作,这应该不难。
  • @Amit:解决方法是能力,但是,如果这不起作用,那么解决方法是与您的经理交谈。如果您无法控制自己的测试环境,他/她会期待什么?
  • 谢谢大家的回复。他们确实有帮助。
【解决方案2】:
case "ie_driver":           

    //IE CODE
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
    cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "https://testvmm6.partnersonline.com/vmm");
    cap.internetExplorer().setCapability("ignoreProtectedModeSettings", true);

    System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"//exe//IEDriverServer1.exe");
    cap.setCapability("IE.binary", "C:/Program Files (x86)/Internet Explorer/iexplore.exe");
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    cap.setJavascriptEnabled(true);
    cap.setCapability("requireWindowFocus", true);
    cap.setCapability("enablePersistentHover", false);

【讨论】:

  • 请在您的答案中添加解释。
  • 请添加一些描述
  • 请设置IE的保护模式,不要使用cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
【解决方案3】:

帮助我的问题是设置初始化页面(IE 11 32 和 64)

 private WebDriver getIEDriver() {
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, INIT_PAGE);

    File file = new File("E:/drivers/IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    return new InternetExplorerDriver(cap);
 }

【讨论】:

  • 我写成:cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "google.com/");
【解决方案4】:

最好的办法是对注册表进行一些调整:

  1. 进入注册表编辑(regedit 从 windows 运行)

  2. HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Zones 下查看您的注册表。在那里,您应该看到 0-4 号键。在这些键 0-4 下,查找名为 2500 的值

  3. 对于从 0 到 4 的所有键,值 2500 具有相同的数据。例如,对于键 0,如果值 2500 的数据为 3(十六进制数据),则使值 2500 的数据为3 表示所有其他键 (1,2,3,4)。

  4. 现在尝试运行脚本。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
相关资源
最近更新 更多