使用Selenium WebDriver实现InternetExplorerDriver时出现了NoSuchElementException。

16

目前,我正在使用WebDriver来调用IE浏览器运行测试。但是,当我尝试运行下面的简单示例时,我收到了一个 NoSuchElementException 错误。

但是,如果我使用Chrome Driver或Firefox driver,代码就能正常工作。任何想法或建议都将不胜感激。

Jar: selenium-server-standalone-2.5.0.jar

Code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public static void main(String[] args) throws InterruptedException {
  DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
  ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
  WebDriver driver = new InternetExplorerDriver(ieCapabilities);
  driver.get("www.google.com");
  driver.findElement(By.name("q"));
}

错误信息:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with name == q (WARNING: The server did not provide any stacktrace information)
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.5.0', revision: '13516', time: '2011-08-23 18:29:57'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_25'
Driver info: driver.version: RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:409)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:197)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:246)
    at org.openqa.selenium.By$ByName.findElement(By.java:298)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:189)
    at lib.WebDriver2.main(WebDriver2.java:14)

那个堆栈跟踪/错误消息看起来不完整,请确认它是否完整? - Jasper
嗨,已更新错误信息 :) IE浏览器被打开并定向到谷歌页面。但是,由于尝试查找元素“q”而失败。 - user836112
同样的问题在这里。我使用的是64位系统,但是使用32位jvm,所以驱动程序也是32位的。下面列出的任何内容都没有帮助。 - Artur Gajowy
1
你明确避免了设置IE的受保护模式设置。这就是InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS功能设置的作用。如果你删除此功能设置并按照项目wiki中所述设置受保护模式设置会发生什么? - JimEvans
1
嗨JimEvans,非常感谢您的帮助!我删除了“ieCapabilities”。相反,我去了Internet选项并按照Project Wiki的步骤进行操作。现在它对我有用了。最重要的信息是“您必须将每个区域的受保护模式设置为相同的值。”再次感谢您的帮助。 - user836112
好的。将此评论提升为答案,以便您可以接受它。 - JimEvans
3个回答

14

你已经明确避免了设置IE的受保护模式。这就是InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS能力设置的作用。当您删除此能力设置并按照项目维基中所述设置受保护模式时,问题似乎会自行解决。


嗨Jim。谢谢你的解决方案。我没有权限修改安全设置。有什么变通的方法吗?谢谢! - sbose
能力就是解决方法。这篇博客文章描述了受保护模式设置的技术原因,以及如果组织不允许您更改设置,则会积极破坏您完成工作的能力。 - JimEvans

5
尝试像下面这样添加隐式等待。另外,正如Robert所说,您的URL应该有http://
WebDriver driver = new InternetExplorerDriver(ieCapabilities);
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);

嗨,Nilesh,非常感谢你的帮助。但是,这个解决方案对我不起作用。 - user836112
那是因为你应该使用FluentWait并带有.ignoring子句来从异常中恢复。 - djangofan

2

项目网站上有一个简短的FAQ条目(大约复制于Selenium 2.9):

The InternetExplorerDriver requires that all security domains are set to the same value (either trusted or untrusted) If you're not in a position to modify the security domains, then you can override the check like this:

DesiredCapabilities capabilities =
DesiredCapabilities.internetExplorer();
capabilities.set(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true); 
WebDriver driver = new InternetExplorerDriver(capabilities);

As can be told by the name of the constant, this may introduce flakiness in your tests. If all sites are in the same protection domain, you should be okay.

并行C# InvalidOperationException消息:

意外错误启动Internet Explorer。必须为所有区域设置相同的保护模式值(启用或禁用)。 (NoSuchDriver)

C#的替代方法而不是调整IE设置(截至2016年2月的最佳猜测):

var ieOptions = new OpenQA.Selenium.IE.InternetExplorerOptions {
                IntroduceInstabilityByIgnoringProtectedModeSettings = true };
using (var driver = new InternetExplorerDriver(ieOptions))
{

这都是 Selenium 问题跟踪器上的第1795号问题的一部分。


1
对于 C# 版本,我无法在 InternetExplorerDriver 构造函数中传递 DesiredCapabilities。 - Popa Andrei

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接