简体   繁体   English

Selenium: JUnit4: WebDriver: IE: 使用 IE 时出现 NoSuchElementException

[英]Selenium: JUnit4: WebDriver: IE: NoSuchElementException when using IE

I recorded my test in Selenium IDE;我在 Selenium IDE 中记录了我的测试; it runs fine in Firefox and Chrome.它在 Firefox 和 Chrome 中运行良好。 When I run it in IE 9, I keep getting this error.当我在 IE 9 中运行它时,我不断收到此错误。 I am new to coding and am having trouble finding an answer to this.我是编码新手,无法找到答案。 If anyone can explain to me in lay man's terms what is happening/why this may be the case, I'd appreciate it.如果有人能用通俗易懂的语言向我解释正在发生的事情/为什么会这样,我将不胜感激。

org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == BODY (WARNING: The server did not provide any stacktrace information)

Java Code: Java代码:

assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Final[\\s\\S]*$"));

I would suggest printing out driver.getPageSource() before the call and check to see if the html that is on the current page matches your expectations.我建议在调用之前打印出 driver.getPageSource() 并检查当前页面上的 html 是否符合您的期望。

If it does match what you'd expect then I'd print out the text content of all of the tags that match By.cssSelector("BODY") and see if any of them contain the text that you'd expect.如果它确实符合您的预期,那么我将打印出所有与By.cssSelector("BODY")匹配的标签的文本内容,并查看其中是否包含您预期的文本。

This will narrow down where the error is - you might need to find another way of finding this element.这将缩小错误所在的范围——您可能需要找到另一种方法来查找此元素。

Internally By uses XPath and casing matters with internet explorer. Internally By 使用 XPath 和大小写与 Internet Explorer 相关。

Try using尝试使用

assertTrue(driver.findElement(By.cssSelector("body")).getText().matches("^[\\s\\S]*Final[\\s\\S]*$"));

instead of代替

assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Final[\\s\\S]

*$")); *$"));

I believe that should work for you.我相信这应该适合你。

IE browser doesn't support Case-insensitive CSS attribute selectors. IE 浏览器不支持不区分大小写的 CSS 属性选择器。 Try to use尝试使用

driver.findElement(By.cssSelector("body"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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