NoSuchElementException发生在Selenium WebDriver中InternetExplorerDriver的实现期间

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

但是,如果我使用Chrome驱动程序或Firefox驱动程序,代码就可以正常工作。任何想法或想法将不胜感激。

罐:selenium-server-standalone-2.5.0.jar

法典:

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)

答案 1

你已明确避免设置 IE 的保护模式设置。这就是功能设置的作用。当您删除此功能设置并设置项目 Wiki 中记录的保护模式设置时,问题似乎自行解决。InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS


答案 2

尝试添加隐式等待,如下所示。另外,正如罗伯特所说,你的URL应该有 http://

WebDriver driver = new InternetExplorerDriver(ieCapabilities);
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);

推荐