简体   繁体   English

Webdriver 不会单击“接受 cookie”按钮,抛出“ElementNotInteractable”异常

[英]Webdriver won't click "Accept cookies" button, throws "ElementNotInteractable" Exception

First of all, I'm using Cucumber 4.2 and Selenium 3 on Firefox 64.首先,我在 Firefox 64 上使用 Cucumber 4.2 和 Selenium 3。

I'm trying to get past the "Accept cookies" button with Selenium, but nothing seems to work.我试图通过 Selenium 跳过“接受 cookie”按钮,但似乎没有任何效果。 I've tried following DebanjanB's solution to this error, but without any luck.我已经尝试按照DebanjanB 的解决方案解决这个错误,但没有任何运气。

Here's my code:这是我的代码:

String btnLocation = new String("/html/body/div[1]/div[2]/div[4]/div[2]/div");
WebDriverWait wait15 = new WebDriverWait(driver, 15);
wait15.until(ExpectedConditions.elementToBeClickable(By.xpath(btnLocation)));
driver.findElement(By.xpath(btnLocation)).click();

And this is the error trace:这是错误跟踪:

    And I have accepted the cookies                     # Stepdefs.i_have_accepted_the_cookies()
  org.openqa.selenium.ElementNotInteractableException: Element <div class="optanon-alert-box-button-middle accept-cookie-container"> could not be scrolled into view
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'DESKTOP-KS4A1NQ', ip: '192.168.1.3', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '9'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 64.0.2, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.23.0, moz:headless: false, moz:processID: 28204, moz:profile: C:\Users\cian\AppData\Loca..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 30433d1c-d85b-45d6-b65e-076d3e04f3ec
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
        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.click(RemoteWebElement.java:84)
        at [org_name].cucumber_ex.Stepdefs.i_have_accepted_the_cookies(Stepdefs.java:55)
        at ?.I have accepted the cookies([org_name]/cucumber_ex/Navigate_Menu_Categories.feature:26)

    When I roll my mouse over a category                # Stepdefs.i_roll_my_mouse_over_a_category()
    And the dropdown menu appears                       # Stepdefs.the_dropdown_menu_appears()
    And I click a subcategory                           # Stepdefs.i_click_a_subcategory()
    Then I should be brought to that subcategory's page # Stepdefs.i_should_be_brought_to_that_subcategory_s_page()

Failed scenarios:
[org_name]/cucumber_ex/Navigate_Menu_Categories.feature:24 # User wants to pick a product category

1 Scenarios (1 failed)
6 Steps (1 failed, 4 skipped, 1 passed)
0m8.878s

org.openqa.selenium.ElementNotInteractableException: Element <div class="optanon-alert-box-button-middle accept-cookie-container"> could not be scrolled into view
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'DESKTOP-KS4A1NQ', ip: '192.168.1.3', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '9'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 64.0.2, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.23.0, moz:headless: false, moz:processID: 28204, moz:profile: C:\Users\cian\AppData\Loca..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 30433d1c-d85b-45d6-b65e-076d3e04f3ec
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
        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.click(RemoteWebElement.java:84)
        at [org_name].cucumber_ex.Stepdefs.i_have_accepted_the_cookies(Stepdefs.java:55)
        at ?.I have accepted the cookies([org_name]/cucumber_ex/Navigate_Menu_Categories.feature:26)

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 9.104 sec <<< FAILURE!
User wants to pick a product category(Navigate menu categories)  Time elapsed: 8.748 sec  <<< ERROR!
org.openqa.selenium.ElementNotInteractableException: Element <div class="optanon-alert-box-button-middle accept-cookie-container"> could not be scrolled into view
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'DESKTOP-KS4A1NQ', ip: '192.168.1.3', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '9'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 64.0.2, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.23.0, moz:headless: false, moz:processID: 28204, moz:profile: C:\Users\cian\AppData\Loca..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 30433d1c-d85b-45d6-b65e-076d3e04f3ec
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
        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.click(RemoteWebElement.java:84)
        at [org_name].cucumber_ex.Stepdefs.i_have_accepted_the_cookies(Stepdefs.java:55)
        at ?.I have accepted the cookies([org_name]/cucumber_ex/Navigate_Menu_Categories.feature:26)


Results :

Tests in error:
  User wants to pick a product category(Navigate menu categories): Element <div class="optanon-alert-box-button-middle accept-cookie-container"> could not be scrolled into view(..)

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  11.302 s
[INFO] Finished at: 2019-01-11T12:41:05Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project cucumber-ex: There are test failures.
[ERROR]
[ERROR] Please refer to C:\Users\cian\Documents\GitHub\Cucumber-Assignment\cucumber-ex\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project cucumber-ex: There are test failures.

Please refer to C:\Users\cian\Documents\GitHub\Cucumber-Assignment\cucumber-ex\target\surefire-reports for the individual test results.
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoFailureException: There are test failures.

Please refer to C:\Users\cian\Documents\GitHub\Cucumber-Assignment\cucumber-ex\target\surefire-reports for the individual test results.
    at org.apache.maven.plugin.surefire.SurefireHelper.reportExecution (SurefireHelper.java:83)
    at org.apache.maven.plugin.surefire.SurefirePlugin.writeSummary (SurefirePlugin.java:176)
    at org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary (SurefirePlugin.java:150)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked (AbstractSurefireMojo.java:650)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute (AbstractSurefireMojo.java:586)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)

I tried changing the third line of my code to:我尝试将代码的第三行更改为:

List<WebElement> elList = wait15.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(btnLocation)));
System.out.println("Elements found: " + elList.size());
for(WebElement el : elList) {
System.out.println(el.toString());
}

...to make sure that element existed, and it did. ...以确保该元素存在,并且确实存在。

What am I doing wrong?我究竟做错了什么?

EDIT: I should add, I used "Inspect element" to get the Xpath of the "Accept cookies" button.编辑:我应该补充一点,我使用“检查元素”来获取“接受cookies”按钮的Xpath。 I've also tried copying that button's class, but when I search for that class in the webpage's source code, not even the word "accept" can be found in it, let alone the class name.我也试过复制那个按钮的类,但是当我在网页的源代码中搜索那个类时,甚至找不到“接受”这个词,更不用说类名了。

EDIT 2.0 To answer Recep's comment, here's the button's HTML:编辑 2.0要回答 Recep 的评论,这里是按钮的 HTML:

<div class="optanon-alert-box-button-middle accept-cookie-container" style="">
<a class="optanon-allow-all accept-cookies-button" title="Accepter les cookies" aria-label="Accepter les cookies" role="button" href="javascript:void(0);" onclick="Optanon.TriggerGoogleAnalyticsEvent('OneTrust Cookie Consent', 'Banner Accept Cookies');" tabindex="3">Accepter les cookies</a>
</div>

the exception states "could not be scrolled into view".异常状态“无法滚动到视图中”。 when you run it, do you actually see the target button on screen?当你运行它时,你真的看到屏幕上的目标按钮了吗? have you tried adding a step to scroll the item into view?您是否尝试添加步骤以将项目滚动到视图中? I don't know how to do that with java and cucumber, but with python it's something like:我不知道如何用 java 和黄瓜来做到这一点,但用 python 是这样的:

driver.execute_script("return arguments[0].scrollIntoView(true);", button_element_to_click)

你可以简单地使用 OneTrust.AllowAll()

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

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