【问题标题】:ElementNotInteractable Exception in SeleniumSelenium 中的 ElementNotInteractable 异常
【发布时间】:2021-01-05 06:43:25
【问题描述】:

我已经在Selenium 上工作了很长一段时间,当我在Chrome v87.0.4280.88 中运行程序时出现错误。我用的Chrome Webdriver的版本和Chrome浏览器是一样的。

TestNG_Demo

@Test
    public void googleSearch() {

    //Go to google.com
    driver.get("https://google.com");

    //Enter text in search textbox
    driver.findElement(By.name("q")).sendKeys("Automation Step by Step");

    //click on search button
    //driver.findElement(By.name("btnK")).click();
    driver.findElement(By.xpath("//input[@name='btnK']")).sendKeys(Keys.RETURN);

}

错误堆栈跟踪

org.openqa.selenium.ElementNotInteractableException: element not interactable
  (Session info: chrome=87.0.4280.88)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'EZL-OA-JACQUELI', ip: '192.168.43.77', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_241'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 87.0.4280.88, chrome: {chromedriverVersion: 87.0.4280.88 (89e2380a3e36c..., userDataDir: C:\Users\lukegoh\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:50405}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 1e96d9d5fffdd9fd39cdd647ab20081f

    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.RemoteWebElement.execute(RemoteWebElement.java:285)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
    at test.TestNG_Demo.googleSearch(TestNG_Demo.java:45)
    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 com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

我已尝试将x-path 用于 Google 搜索按钮,但仍然给我错误。我可以知道如何解决此问题吗?

【问题讨论】:

    标签: java selenium xpath selenium-chromedriver


    【解决方案1】:

    因为你在谷歌搜索文本中输入一些内容后,它会出现建议,这是让你的按钮目标By.name("btnK")被覆盖。

    简单的解决方案你可以在调用发送.sendKeys("")后调用.submit()

    driver.findElement(By.name("q")).sendKeys("Automation Step by Step");
    driver.findElement(By.name("q")).submit();
    

    文本输入前后的图像

    【讨论】:

      【解决方案2】:

      你使用过的定位器:

      driver.findElement(By.xpath("//input[@name='btnK']"))
      

      无法唯一标识 Google 搜索,并且您的程序正在尝试与错误的元素进行交互。

      因此您会看到错误。

      作为替代方案,您可以使用以下Locator Strategies

      WebElement element = driver.findElement(By.name("q"));
      element.sendKeys("Automation Step by Step");
      element.sendKeys(Keys.RETURN);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-06
        • 1970-01-01
        • 1970-01-01
        • 2020-11-26
        • 2020-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多