Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE7 - NoSuchElementException with Selenium

I'm trying to use Selenium on IE7 with InternetExplorerDriver on Windows XP. This code works fine with Firefox, IE9 and even IE9 in compatibility mode (on W7).

HTML:

<HTML xml:lang="fr" xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE></TITLE></HEAD>
<BODY>
<div id="login">chicken</div>
</BODY>

Building driver:

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;
}

And trying to get my #index element:

log.info(driver.getPageSource());

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

Page source is fetch properly, but anytime I try to access an element, I get a org.openqa.selenium.NoSuchElementException. I also tried by id and XPath.

Any idea of what's going wrong ?

PS: In Windows XP, there is no security mode for IE.

EDIT: driver.getPageSource() returns:

<HTML xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr"><HEAD><TITLE></TITLE></HEAD>
<BODY>
<DIV id=login>chicken</DIV></BODY></HTML>
like image 746
Arthur Avatar asked Nov 12 '13 15:11

Arthur


People also ask

What is NoSuchElementException selenium?

What is a NoSuchElementException in Selenium? NoSuchElementException is one of the most common exceptions in Selenium WebDriver, and it is thrown when an HTML element cannot be found. A NoSuchElementException occurs when the Selenium locator strategy defined is unable to find the desired HTML element in the web page.

How do I overcome NoSuchElementException?

The exception occurs when WebDriver is unable to find and locate elements. Usually, this happens when the tester writes incorrect element locator in the findElement(By, by) method. with the spaces and verify using Try XPath then we can avoid this exception.


1 Answers

Ok - per the comments, you specified that you weren't running against the hub, yet, in your code - you point to a hub. I think it has something to do with this.

Change this block of code:

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

to:

driver = new IEDriver(capabilities);

RemoteWebDriver(new URL("http://localhost:4444/wd/hub") this piece here is pointing your test to some remote driver, which means "a selenium node that is connected to a hub." You've already confirmed that this you are not using a hub, so this answer should solve your issue.

A breakdown:

RemoteWebDriver() = Selenium Node that is connected to a hub
ChromeDriver|IEDriver|FirefoxDriver = Local browser session
like image 113
ddavison Avatar answered Oct 26 '22 19:10

ddavison