简体   繁体   中英

“No element is found” exception in WebDriver running on Internet Explorer using Java

we want to move our tests to selenium 2 and i have found an issue which i don't know how to resolve it.

I am using the following commands for webdriver:

WebDriver driver = new InternetExplorerDriver();
driver.navigate().to("webapp");
Thread.sleep(3000);
System.out.println(driver.getPageSource());
WebElement element = driver.findElement(By.id("someid"));

At the last line exception is raised and no element is found. The same example works well in firefox, but we need it to have it in IE. I have tried to add more sleep, but it doesn't help. getPageSource method returns correct html.

I have also tried to get body tag, with the following command, but it returns null.

List<WebElement> list = driver.findElements(By.tagName("body"));

Our web application is created in gwt.

Do you know what may cause that selenium doesn't see any element?

Try using a smart wait like this before making your assignment statement. You just need to pass in your current WebDriver object and a timeout value.

 new WebDriverWait(driver, timeOutInSeconds).until(new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(final WebDriver theDriver) {
            return Boolean.valueOf(theDriver.findElements(By.id(elementId)).size() > 0);
        }
    });

The code you posted is absolutely problem-free.

Make sure you use some of the newer Selenium versions and not some old alpha/beta version. Which IE do you use, does it have any special add-ons? Does it work with some minimal web pages like this?

<html>
<input id='myId' />
</html>

Have you tried searching for another element? By some other technique like xpath or css selector? Is your capitalization absolutely right? If you save the html of the page and try it loading locally with said testcase, does it work? Could you provide a minimal test case? As a unlikely-to-work last resort solution, you can try to fallback to Selenium 1 for two lines of code that help waiting in some rare cases:

Selenium sele = new WebDriverBackedSelenium(driver, "webapp");
sele.waitForPageToLoad("10000");

maybe you can try to add

driver.switchTo().window(driver.getWindowHandle());

before searching for elements. I had this issue when Webdriver moved between pages on different servers (not with IE but with Firefox).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM