【问题标题】:Selenium Issue with Internet Explorer : org.openqa.selenium.NoSuchElementException : Unable to find element with xpathInternet Explorer 的 Selenium 问题:org.openqa.selenium.NoSuchElementException:无法使用 xpath 找到元素
【发布时间】:2021-03-24 22:32:35
【问题描述】:

我正在尝试在 Internet Explorer (IE 11) 中为我的系统运行 Selenium(Selenium 独立服务器 3.141.59)。在登录之前一直有效。

登录后它会打开一个新窗口,我使用以下行打开所需的页面。

driver.get("http://appurl/function1.jsp");

之后我找不到任何元素,甚至通过 xpath 也找不到,因为我在下面收到此错误。

org.openqa.selenium.NoSuchElementException : 无法找到元素 with xpath == //输入[@value='Continue']

  @BeforeClass(alwaysRun = true)
  public void setUp() throws Exception {
    DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
    cap.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.DISMISS);
    InternetExplorerOptions opt = new InternetExplorerOptions();
    opt.merge(cap);
    System.setProperty("webdriver.ie.driver", "D:\\JavaProjects\\Test\\libs\\IEDriverServer.exe");
    driver = new InternetExplorerDriver(opt);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

有人可以帮我解决这个问题吗?非常感谢您。


更新

我按照建议进行了以下更改。

driver.get("http://appurl/function1.jsp");
// after login to system I navigate to the page in above URL
new WebDriverWait(driver, 40).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='Continue']"))).click();
// waiting for the button with value as 'Continue' to be clickable

但我发现没有成功,因为问题仍然存在。

我也在下面添加日志。

RemoteTestNG] detected TestNG version 7.0.1
Started InternetExplorerDriver server (32-bit)
3.9.0.0
Listening on port 16289
Only local connections are allowed
Jan 25, 2021 4:07:44 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
FAILED: testLogintoImmunizationLive
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //input[@value='Continue'] (tried for 40 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at com.immunization.IMMtestLogin.testLogintoImmunizationLive(IMMtestLogin.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.testng.TestRunner.privateRun(TestRunner.java:766)
    at org.testng.TestRunner.run(TestRunner.java:587)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
    at org.testng.TestNG.runSuites(TestNG.java:1039)
    at org.testng.TestNG.run(TestNG.java:1007)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //input[@value='Continue']
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'IT-PC-X', ip: 'IP_ADDRESS', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:16289/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss}
Session ID: 8e60910f-bfeb-4580-92bb-ddd8e7d06c84
*** Element info: {Using=xpath, value=//input[@value='Continue']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201)
    at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:641)
    at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:638)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
    ... 29 more


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================

【问题讨论】:

    标签: java selenium internet-explorer-11 webdriverwait selenium-iedriver


    【解决方案1】:

    此错误消息...

    Started InternetExplorerDriver server (32-bit)
    3.9.0.0
    Listening on port 16289
    Only local connections are allowed
    Jan 25, 2021 4:07:44 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    FAILED: testLogintoImmunizationLive
    org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //input[@value='Continue'] (tried for 40 second(s) with 500 milliseconds interval)
    

    ...暗示IEDriverServer 无法在 WebBrowsing 会话InternetExplorer 浏览器 会话中找到所需的 WebElement

    您的主要问题是您使用的二进制文件版本之间的不兼容性,如下所示:

    • 虽然您的 Selenium 独立服务器 版本是 3.141.59,它是最新发布的候选版本。
    • 但是您的 InternetExplorerDriver 版本是 3.9.0.0

    因此,Selenium 独立服务器 v3.141.59InternetExplorerDriver v3.9.0.0 之间存在明显的不匹配。根据 最佳实践,由于 Selenium ClientInternetExplorerDriver 是同步发布的,因此您必须使用同一版本中的两个二进制文件。


    其他信息

    另外 InternetExplorerDriver 服务器 v3.9.0.0 根据以下参考资料存在已知问题:

    此问题已通过以下提交解决:


    解决方案

    注意:根据最佳实践Selenium ClientInternetExplorerDriver 是同步发布的,您必须尝试使用来自同一主要版本的两个二进制文件。

    • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。
    • 执行您的@Test
    • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例。

    【讨论】:

      【解决方案2】:

      当您尝试在get() 之后与click() 进行交互时,您需要为elementToBeClickable() 诱导WebDriverWait 所需的元素,您可以使用以下任一Locator Strategies

      • cssSelector

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[value='Continue']"))).click();
        
      • xpath

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='Continue']"))).click();
        

      参考

      你可以在NoSuchElementException找到详细讨论:

      【讨论】:

      • 非常感谢您的解释,但还是不行。我用日志更新了上面的问题
      【解决方案3】:

      您可能需要使用等待功能,直到在 DOM 页面上可以点击 continueInput 元素。代码如下;

      continueInput = WebDriverWait(driver,40).until(EC.element_to_be_clickable((By.XPATH, '//input[@value='Continue']')))
      

      【讨论】:

      • 非常感谢..但问题仍然存在
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      相关资源
      最近更新 更多