NoSuchElementException:在 Microsoft 的示例中使用 Selenium 4 在 IE 模式下运行 Edge 驱动程序时无法找到元素错误

问题描述 投票:0回答:2

我正在使用:

  • 硒版本4.3.0
  • IE驱动4.3.0(32位)
  • 边缘版本103.0.1264.71
  • Java(JRE 1.8)

我正在尝试在 IE 模式下获得一个简单的 Edge 驱动程序示例,该示例完全来自 Microsoft 网站 https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/ie-mode ?tabs=java#the-complete-sample

唯一额外的一行是添加的IE驱动属性

System.setProperty("webdriver.ie.driver", C:\\Downloads\\IEDriverServer_Win32_4.3.0\\IEDriverServer.exe");

我还在这里查看了所有 Selenium IE 驱动程序配置: https://www.selenium.dev/documentation/ie_driver_server/#required-configuration

我现在在 IE 模式下启动 Edge,似乎正确加载驱动程序和页面,但尝试在页面上查找元素(通过 id 或 xpath)会导致错误。

错误输出:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Started InternetExplorerDriver server (32-bit)
4.3.0.0
Only local connections are allowed
Jul. 26, 2022 4:29:55 P.M. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected upstream dialect: W3C
Jul. 26, 2022 4:29:55 P.M. org.openqa.selenium.remote.ProtocolHandshake createSession
WARNING: Support for Legacy Capabilities is deprecated; You are sending the following invalid capabilities: [ie.edgechromium, ie.edgepath]; Please update to W3C Syntax: https://www.selenium.dev/blog/2022/legacy-protocol-support/
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #sb_form_q
For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element
Build info: version: '4.3.0', revision: 'a4995e2c09*'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Command: [5d0f617f-9996-4877-a367-834c673c819a, findElement {using=id, value=sb_form_q}]
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.edgechromium: true, ie.edgepath: C:\Program Files (x86)\Micr..., ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:64126/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 5d0f617f-9996-4877-a367-834c673c819a
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569)
    at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:162)
    at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:66)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:387)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:379)
    at junk.main(junk.java:22)

我不确定为什么会收到这条线:

You are sending the following invalid capabilities: [ie.edgechromium, ie.edgepath];

因为它直接来自示例,并且我确保框架版本都是正确的(据我所知)。

我不知道为什么它无法找到该元素,即使在创建驱动程序后进行了短暂的睡眠。

java selenium selenium-webdriver selenium-iedriver microsoft-edge-chromium
2个回答
0
投票

这个错误信息...

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #sb_form_q

...意味着在搜索所需的 WebElement 时引发了 NoSuchElementException


解决方案

理想情况下,要将字符序列发送到 website 中的 Search Box 字段,因为该元素是动态元素,您需要为 elementToBeClickable() 引入 WebDriverWait,并且可以使用以下任一方法以下定位器策略

  • 使用名称

    driver.get("https://www.bing.com/");
    new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.name("q"))).sendKeys("WebDriver", Keys.RETURN);
    
  • 使用cssSelector

    driver.get("https://www.bing.com/");
    new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.sb_form_q"))).sendKeys("WebDriver", Keys.RETURN);
    
  • 使用xpath

    driver.get("https://www.bing.com/");
    new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='sb_form_q']"))).sendKeys("WebDriver", Keys.RETURN);
    
  • 浏览器快照:

bing_search


0
投票

还有更新吗?我现在遇到了同样的问题,每当用 IE 模式打开时,开发工具和 selenium 都找不到元素

© www.soinside.com 2019 - 2024. All rights reserved.