Selenium:Internet Explorer中的NoSuchElementException

3

我正在尝试将一份使用Selenium IDE编写的内部Web应用程序脚本,转移到使用Java中RemoteWebDrivers的Selenium Grid环境。

这份脚本在Chrome和Firefox上能够正常运行,但是在Internet Explorer上却不行。每次当我试图在页面中查找一个元素时,我只得到一个NoSuchElementException异常(页面本身可以正常加载)。

以下是使用维基百科页面展示相同行为的示例代码:

public class Wikipedia_test_IE {
 private WebDriver driver = null;
 private String baseUrl = null;
 private boolean acceptNextAlert = true;
 private StringBuffer verificationErrors = new StringBuffer();


 @Before
 public void setUp() throws Exception {
//      DesiredCapabilities cap = DesiredCapabilities.firefox();
 DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
 cap.setCapability(CapabilityType.VERSION, "9");

        driver = new RemoteWebDriver (new URL("http://192.168.1.230:4444/wd/hub"), cap);    
        baseUrl = "https://en.wikipedia.org/wiki/Rosetta_%28spacecraft%29";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 }

 @Test
 public void testAKTargobankTestfall1KN() throws Exception {
      driver.get(baseUrl);
      driver.findElement(By.linkText("space probe")).submit();
 }
}

我尝试使用findElement(By.Id)和findElement(By.cssSelector)但没有效果。在页面加载和选择之间加入延迟也没有效果,使用click而不是submit也一样。我已经启用了IE驱动程序的调试日志记录,但输出并不能帮助我理解问题所在,希望有人能够提供帮助。
server.cc(239) Command: POST /session {"desiredCapabilities":{"platform":"WINDOWS","ensureCleanSession":true,"browserName":"internet explorer","version":"9"}}
IESession.cpp(43) Mutex acquired for session initalization
IESession.cpp(105) Releasing session initialization mutex
command.cc(33) Raw JSON command: { "command" : "newSession", "locator" : { }, "parameters" : {"desiredCapabilities":{"platform":"WINDOWS","ensureCleanSession":true,"browserName":"internet explorer","version":"9"}} }
BrowserFactory.cpp(68) Ignoring Protected Mode Settings: 0
BrowserFactory.cpp(71) Checking validity of Protected Mode settings.
BrowserFactory.cpp(984) Detected IE version: 9, detected Windows version: 6
BrowserFactory.cpp(1074) Found Protected Mode setting value of 0 for zone 1
BrowserFactory.cpp(1074) Found Protected Mode setting value of 0 for zone 2
BrowserFactory.cpp(1074) Found Protected Mode setting value of 0 for zone 3
BrowserFactory.cpp(1074) Found Protected Mode setting value of 0 for zone 4
BrowserFactory.cpp(74) Has Valid Protected Mode Settings: 1
BrowserFactory.cpp(170) Starting IE using the IELaunchURL API
BrowserFactory.cpp(124) IE launched successfully with process ID 2188
BrowserFactory.cpp(130) Process with ID 2188 is executing iexplore.exe
BrowserFactory.cpp(313) Ignoring zoom setting: 0
BrowserFactory.cpp(581) Browser zoom level is 100%
IECommandExecutor.cpp(650) Persistent hovering set to: 1
ProxyManager.cpp(118) Using existing system proxy settings.
server.cc(308) Response: {"sessionId":"4aba017b-91e9-4f48-9f9f-f38b0464bb6b","status":303,"value":"/session/4aba017b-91e9-4f48-9f9f-f38b0464bb6b"}


server.cc(239) Command: GET /session/4aba017b-91e9-4f48-9f9f-f38b0464bb6b {}
command.cc(33) Raw JSON command: { "command" : "getSessionCapabilities", "locator" : { "sessionid" : "4aba017b-91e9-4f48-9f9f-f38b0464bb6b" }, "parameters" : {} }
IECommandExecutor.cpp(544) No alert handle is found
server.cc(308) Response: {"sessionId":"4aba017b-91e9-4f48-9f9f-f38b0464bb6b","status":0,"value":{"browserAttachTimeout":0,"browserName":"internet explorer","cssSelectorsEnabled":true,"elementScrollBehavior":0,"enableElementCacheCleanup":true,"enablePersistentHover":true,"handlesAlerts":true,"ie.browserCommandLineSwitches":"","ie.ensureCleanSession":false,"ie.forceCreateProcessApi":false,"ie.usePerProcessProxy":false,"ignoreProtectedModeSettings":false,"ignoreZoomSetting":false,"initialBrowserUrl":"http://localhost:15278/","javascriptEnabled":true,"nativeEvents":true,"platform":"WINDOWS","requireWindowFocus":false,"takesScreenshot":true,"unexpectedAlertBehaviour":"dismiss","version":"9"}}

server.cc(239) Command: POST /session/4aba017b-91e9-4f48-9f9f-f38b0464bb6b/timeouts/implicit_wait {"ms":30000}
command.cc(33) Raw JSON command: { "command" : "implicitlyWait", "locator" : { "sessionid" : "4aba017b-91e9-4f48-9f9f-f38b0464bb6b" }, "parameters" : {"ms":30000} }
IECommandExecutor.cpp(544) No alert handle is found
server.cc(308) Response: {"sessionId":"4aba017b-91e9-4f48-9f9f-f38b0464bb6b","status":0,"value":null}

server.cc(239) Command: POST /session/4aba017b-91e9-4f48-9f9f-f38b0464bb6b/url {"url":"https://en.wikipedia.org/wiki/Rosetta_%28spacecraft%29"}
command.cc(33) Raw JSON command: { "command" : "get", "locator" : { "sessionid" : "4aba017b-91e9-4f48-9f9f-f38b0464bb6b" }, "parameters" : {"url":"https://en.wikipedia.org/wiki/Rosetta_%28spacecraft%29"} }
IECommandExecutor.cpp(544) No alert handle is found
Browser.cpp(421) Navigate Events Completed.
Browser.cpp(452) Browser ReadyState is not '4', indicating 'Complete'; it was 1
Browser.cpp(421) Navigate Events Completed.
Browser.cpp(437) Browser busy property is true.

(The last two lines repeat several times)

ElementRepository.cpp(107) Refreshing managed element cache. Found 0 to remove from cache.
Browser.cpp(421) Navigate Events Completed.
Browser.cpp(476) Waiting for document to complete...
Browser.cpp(481) Not in navigating state
server.cc(308) Response: {"sessionId":"4aba017b-91e9-4f48-9f9f-f38b0464bb6b","status":0,"value":null}

server.cc(239) Command: POST /session/4aba017b-91e9-4f48-9f9f-f38b0464bb6b/element {"using":"link text","value":"space probe"}
command.cc(33) Raw JSON command: { "command" : "findElement", "locator" : { "sessionid" : "4aba017b-91e9-4f48-9f9f-f38b0464bb6b" }, "parameters" : {"using":"link text","value":"space probe"} }
IECommandExecutor.cpp(544) No alert handle is found
ElementFinder.cpp(49) Using FindElement atom to locate element having linkText = space probe
Browser.cpp(114) No child frame focus. Focus is on top-level frame
Script.cpp(485) -2147024891 [Zugriff verweigert]: Unable to execute code, call to IHTMLWindow2::execScript failed
Script.cpp(171) Cannot create anonymous function
ElementFinder.cpp(84) Unexpected error attempting to find element by mechanism linkText with criteria space probe

有任何想法吗?


你运行的是IE9浏览器吗?那个错误听起来像是JS执行被某种方式阻止了。也许可以检查一下你的安全设置?同时确认一下是否在其他网站上也会出现这个问题?或者尝试在IE的开发者控制台中使用CSS选择器,以确保像这样基本的东西实际上能够正常工作? - Arran
5个回答

1
感谢您的帮助。回答一些上面提到的问题,这些问题本应该在原始帖子中包含:
- 是的,确实是IE 9 - 我已经在所有安全区域中设置了启用保护模式 - 使用点击而不是提交没有任何区别(最初)
恰好在运行Selenium节点的VM在晚上应用了安全更新,其中包括IE的一个更新。
起初,我收到的错误消息与之前不同 - IE无法找到刚打开的浏览器窗口,并且窗口也没有关闭。所有安全设置都被更新重置。
重新启用保护模式后,现在一切正常。正如Richard所指出的那样,我确实需要将查找元素更改为.click()而不是.submit()。
所以,现在一切都正常了,我不知道为什么。再次感谢!

0

你应该使用click()而不是submit()

driver.findElement(By.linkText("space probe")).click();

0

我也曾在测试中遇到过这个错误,这些测试在Firefox上运行良好,但在IE上却不行。

我可以建议你:

  • 检查IE的受保护模式设置。
  • 禁用IE驱动程序上的本地事件。
  • 在这些特定情况下使用JavaScript(在我看来是最糟糕的一种方法)。

这是IE驱动程序已知的问题。

http://jimevansmusic.blogspot.be/2012/08/youre-doing-it-wrong-protected-mode-and.html http://jimevansmusic.blogspot.be/2013/01/revisiting-native-events-in-ie-driver.html


0

尝试使用InternetExplorerDriver而不是RemoteWebDriver,通常不会有任何区别,但在IE中,一切皆有可能-.-

我遇到了类似的问题,最终我使用了以下代码,它对我有效:

@Before
public void setUp() {
   //... some irrelevant Code ... 

   System.setProperty("webdriver.ie.driver", "Path to IEDriver.exe");
   InternetExplorerDriver driver = new InternetExplorerDriver();

   //... some irrelevant Code ...
}

0
你可以按照以下方式编写代码:
while(true) {
    try {
        driver.findElement(By.linkText("space probe")).submit();
        break;
    } catch (Exception e) {
        continue;
    }
}

我这样编写代码是为了让线程一直搜索,直到成功。使用 implicitlyWait 时,浏览器只会等待最长时间。但有可能您的浏览器页面已经加载完成,但所需部分是通过 ajax 调用加载的。在这种情况下,您的程序将抛出错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接