IE7 - 使用Selenium的NoSuchElementException

Art*_*hur 7 java selenium internet-explorer xpath

我试图在IE7 上使用Selenium和Windows XP 上的InternetExplorerDriver.此代码适用于兼容模式下的Firefox,IE9甚至IE9(在W7上).

HTML:

<HTML xml:lang="fr" xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE></TITLE></HEAD>
<BODY>
<div id="login">chicken</div>
</BODY>
Run Code Online (Sandbox Code Playgroud)

建筑司机:

private static WebDriver getIE7WebDriver() {
    WebDriver driver = null;
    DesiredCapabilities capabilities;
    capabilities= DesiredCapabilities.internetExplorer();
    capabilities.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, false);
    capabilities.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS,true);
    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "internet explorer");
    capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
    capabilities.setCapability(CapabilityType.VERSION, "7");

    System.setProperty("webdriver.ie.driver",(new File("C:\\selenium\\IEDriverServer.exe")).getAbsolutePath());
    try {
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return driver;
}
Run Code Online (Sandbox Code Playgroud)

并尝试获取我的#index元素:

log.info(driver.getPageSource());

try {
    String value = driver.findElement(By.cssSelector("#login")).toString();
    log.info(value);
}
catch ( Exception e ) {
    log.error(e.toString());
}
Run Code Online (Sandbox Code Playgroud)

页面源是正确获取的,但无论何时我尝试访问一个元素,我都会得到一个org.openqa.selenium.NoSuchElementException.我也尝试过通过idXPath.

什么出错了?

PS:在Windows XP中,IE没有安全模式.

编辑:driver.getPageSource()返回:

<HTML xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr"><HEAD><TITLE></TITLE></HEAD>
<BODY>
<DIV id=login>chicken</DIV></BODY></HTML>
Run Code Online (Sandbox Code Playgroud)

dda*_*son 1

好的 - 根据评论,您指定您没有针对集线器运行,但是,在您的代码中 - 您指向一个集线器。我认为这与此有关。

更改此代码块:

try {
    driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

到:

driver = new IEDriver(capabilities);
Run Code Online (Sandbox Code Playgroud)

RemoteWebDriver(new URL("http://localhost:4444/wd/hub")这里的这一部分将您的测试指向某个远程驱动程序,这意味着“连接到集线器的硒节点”。您已经确认您没有使用集线器,因此这个答案应该可以解决您的问题。

细分:

RemoteWebDriver() = Selenium Node that is connected to a hub
ChromeDriver|IEDriver|FirefoxDriver = Local browser session
Run Code Online (Sandbox Code Playgroud)