【问题标题】:NoSuchElementException : Unable to find element with id / Works fine in FireFox and Chrome but not in IENoSuchElementException:无法找到具有 id 的元素/在 FireFox 和 Chrome 中可以正常工作,但在 IE 中无法正常工作
【发布时间】:2015-09-18 11:45:47
【问题描述】:

我在 Eclipse 中使用以下代码,该代码在 FireFox 和 Chrome 中都可以正常工作(显然我在这些测试用例中没有调用 IE webdriver),但在 IE10 中却没有,代码在 assertTrue 部分失败。所以下面的代码会打开一个 IE 浏览器窗口,打开请求的 URL,填写用户名和密码,然后单击 OK 按钮。登录成功,我想确认我是以我使用的用户名和密码登录的用户。

public void test3() throws Exception {
System.setProperty("webdriver.ie.driver", "browsers\\IEDriverServer.exe");

/** Test: Start Browser*/
driver=new InternetExplorerDriver();
driver.manage().window().maximize();

/** Test: Login is possible*/
driver.get ("http://URL/");
driver.findElement(By.id("Username")).clear();
driver.findElement(By.id("Username")).sendKeys("testname");
driver.findElement(By.id("Password")).clear();
driver.findElement(By.id("Password")).sendKeys("Welcome01");
driver.findElement(By.xpath("//input[@id='']")).click();

/** Test: logged in as user confirmation*/
assertTrue(driver.findElement(By.id("dvPageOptions")).getText().contains("testname"));

driver.quit();

这会引发错误:

org.openqa.selenium.NoSuchElementException:无法找到元素 id == dvPageOptions(警告:服务器没有提供任何 stacktrace 信息)命令持续时间或超时:274 毫秒 有关此错误的文档,请访问: http://seleniumhq.org/exceptions/no_such_element.html 构建信息: 版本:'2.47.1',修订:'411b314',时间:'2015-07-30 02:56:46' 系统信息:主机:'BA91-CNU21923BJ',ip:'10.55.17.73',os.name: 'Windows 7',os.arch:'x86',os.version:'6.1',java.version: '1.8.0_31' 驱动信息:org.openqa.selenium.ie.InternetExplorerDriver

这是页面来源的一部分

    < div id="dvPageOptions" style='display:inline;position:absolute;right:10px'>
logged in as: testname</div >

如您所见,我要断言的 id 和内容在源代码中。

附加信息: 我使用的是 IE10 并且安全设置都正确(所有区域都启用了保护模式)

【问题讨论】:

  • 好像IE10浏览器有问题。你在IE11中试过吗? dvPageOptions div 是否在 IE 浏览器中加载有点晚,您可以手动测试并查看?
  • 网页在不同浏览器之间的行为可能不同。例如,您要导航的 URL 在 InternetExplorer 中的打开方式可能不同。您可以尝试获取 driver.getPageSource() 并检查 id = 'dvPageOptions' 的元素是否确实存在于 Internet Explorer 页面中。

标签: java eclipse internet-explorer selenium selenium-webdriver


【解决方案1】:

谢谢你,Girish Sortur!正如你提到的,id 加载有点晚,所以我决定加入一个等待动作(见下文)。现在它工作正常!

WebDriverWait wait = new WebDriverWait(driver,120);
wait.until(ExpectedConditions.elementToBeClickable(By.id("GoTo")));
assertTrue(driver.findElement(By.id("dvPageOptions")).getText().contains("testname"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多