scala 当元素可见且可点击时,Selenium Web 驱动程序无法点击按钮,抛出 org.openqa.selenium.ElementNotInteractableException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44101284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:15:26  来源:igfitidea点击:

Selenium web driver cannot click a button while element is visible and clickable, throws org.openqa.selenium.ElementNotInteractableException

javahtmlscalaseleniumselenium-webdriver

提问by user4955663

I have following menu bar which I try to test with selenium web driver:

我有以下菜单栏,我尝试使用 selenium web 驱动程序进行测试:

<div class="text-center">
  <div class="btn-group pull-left">
    <button type="button" class="btn btn-default" id="id-home_prevButton" style="min-height:10px;min-width:10px" hidden="false"><span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>
    </button><button type="button" class="btn btn-default" id="id-home_nextButton" style="min-height:10px;min-width:10px" hidden="false"><span class="glyphicon glyphicon-arrow-right" aria-hidden="true"></span></button>
  </div>
  <span class="span-title" id="id-home_title">Home</span>
  <div class="btn-group pull-right">
    <button type="button" class="btn btn-default" id="id-home_infoButton" hidden="false"><span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span></button>
    <div class="btn-group">
      <button type="button" id="id-home_dropDownButton" class="btn btn-default dropdown-toggle" data-toggle="dropdown" hidden="false">
        <i class="glyphicon glyphicon-align-justify"></i><span class="caret"></span></button>
      <ul class="dropdown-menu dropdown-menu-right simple-title-dropdown" role="menu">
        <li><a href="#home" id="id-home_menu_homeLink">Home</a></li>
      </ul>
    </div>
  </div>
</div>

Button ids are unique.

按钮 ID 是唯一的。

Selenium scala code clicking the button is:

单击按钮的 Selenium scala 代码是:

def wEPrint(description: String, wE: WebElement): String = {
  s"$description = wE.getTagName: ${wE.getTagName}  wE.getText: ${wE.getText}  wE.getSize: ${wE.getSize}  wE.isDisplayed: ${wE.isDisplayed}  wE.isEnabled: ${wE.isEnabled}  wE.isSelected: ${wE.isSelected}"
}

...


log.debug("Find id-home_prevButton")

import org.openqa.selenium.support.ui.WebDriverWait
val waitPrevButton = new WebDriverWait(driver, 20)
val prevButton = driver.findElement(By.id("id-home_prevButton"))
log.debug(wEPrint("prevButton",prevButton))
waitPrevButton.until(ExpectedConditions.visibilityOfElementLocated(By.id("id-home_prevButton")))
log.debug("id-home_prevButton.click")
prevButton.click()

sbt console output is:

sbt 控制台输出是:

22:49:11.378 [run-main-3] DEBUG com.example.selex.MWLExp$ - Find id-home_prevButton
22:49:11.539 [run-main-3] DEBUG com.example.selex.MWLExp$ - prevButton = wE.getTagName: button  wE.getText:   wE.getSize: (40, 34)  wE.isDisplayed: true  wE.isEnabled: true  wE.isSelected: false
22:49:11.588 [run-main-3] DEBUG com.example.selex.MWLExp$ - id-home_prevButton.click
[error] (run-main-3) org.openqa.selenium.ElementNotInteractableException: 
[error] Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
[error] System info: host: 'tra', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-72-generic', java.version: '1.8.0_121'
[error] Driver info: org.openqa.selenium.firefox.FirefoxDriver
[error] Capabilities [{moz:profile=/tmp/rust_mozprofile.T6j4aNCkep6a, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0, platformVersion=4.4.0-72-generic, moz:processID=51151.0, browserName=firefox, javascriptEnabled=true, platformName=linux}]
[error] Session ID: 1349d85f-d360-4725-963c-f6ec277e308c
org.openqa.selenium.ElementNotInteractableException: 
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'tra', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-72-generic', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=/tmp/rust_mozprofile.T6j4aNCkep6a, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0, platformVersion=4.4.0-72-generic, moz:processID=51151.0, browserName=firefox, javascriptEnabled=true, platformName=linux}]
Session ID: 1349d85f-d360-4725-963c-f6ec277e308c
    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:150)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:272)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:82)
    at com.example.selex.MWLExp$.run01(MWLExp.scala:92)
    at simple.MainJVM$.main(DemoJVM.scala:44)
    at simple.MainJVM.main(DemoJVM.scala)
    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)

Selenium test code can find the button, it is displayed and enabled, visibilityOfElementLocated succeeds but clicking throws ElementNotInteractableException.

Selenium 测试代码可以找到按钮,它被显示并启用,visibilityOfElementLocated 成功但点击抛出ElementNotInteractableException。

When I run the app without selenium, button is visible and I can click it and it works without any errors.

当我在没有 selenium 的情况下运行应用程序时,按钮是可见的,我可以单击它并且它可以正常工作而没有任何错误。

In selenium IDE clicking the button works OK. Selenium IDE Java code for clicking the button is:

在 selenium IDE 中,单击按钮可以正常工作。单击按钮的 Selenium IDE Java 代码是:

driver.findElement(By.id("id-home_prevButton")).click();

EDIT

编辑

I modified the code slightly:

我稍微修改了代码:

import org.openqa.selenium.support.ui.WebDriverWait
val waitPrevButton = new WebDriverWait(driver, 20)
val prevButton = driver.findElement(By.id("id-home_prevButton"))
log.debug(wEPrint("prevButton",prevButton))
waitPrevButton.until(ExpectedConditions.visibilityOfElementLocated(By.id("id-home_prevButton")))
log.debug("id-home_prevButton.click")
//prevButton.click()
//prevButton.sendKeys(Keys.RETURN)
prevButton.sendKeys(Keys.ENTER)

And now the exception message is:

现在异常消息是:

23:30:56.497 [run-main-6] DEBUG com.example.selex.MWLExp$ - Find id-home_prevButton
23:30:56.735 [run-main-6] DEBUG com.example.selex.MWLExp$ - prevButton = wE.getTagName: button  wE.getText:   wE.getSize: (40, 34)  wE.isDisplayed: true  wE.isEnabled: true  wE.isSelected: false
23:30:56.798 [run-main-6] DEBUG com.example.selex.MWLExp$ - id-home_prevButton.click
[error] (run-main-6) org.openqa.selenium.ElementNotInteractableException: Element is not visible
[error] Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
[error] System info: host: 'tra', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-72-generic', java.version: '1.8.0_121'
[error] Driver info: org.openqa.selenium.firefox.FirefoxDriver
[error] Capabilities [{moz:profile=/tmp/rust_mozprofile.gBopOPHPUrCL, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0, platformVersion=4.4.0-72-generic, moz:processID=54936.0, browserName=firefox, javascriptEnabled=true, platformName=linux}]
[error] Session ID: eba7323c-6412-4ed7-a91f-3f897b3ec83f
org.openqa.selenium.ElementNotInteractableException: Element is not visible
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'tra', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-72-generic', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=/tmp/rust_mozprofile.gBopOPHPUrCL, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0, platformVersion=4.4.0-72-generic, moz:processID=54936.0, browserName=firefox, javascriptEnabled=true, platformName=linux}]
Session ID: eba7323c-6412-4ed7-a91f-3f897b3ec83f
    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:150)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:272)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:96)
    at com.example.selex.MWLExp$.run01(MWLExp.scala:93)
    at simple.MainJVM$.main(DemoJVM.scala:44)
    at simple.MainJVM.main(DemoJVM.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

So now it says that element is not visible although visibilityOfElementLocated has succeeded and it is displayed and enabled.

所以现在它说该元素不可见,尽管visibilityOfElementLocated 已经成功并且它被显示和启用。

What's the reason for this exception and how to fix it?

此异常的原因是什么以及如何修复它?

Thank you for your support.

谢谢您的支持。

回答by Bharadhwaj p

Use javascriptExecutor to click the element by embedding javascript. By using javascript executor we can run the javascript on the driver.

使用 javascriptExecutor 通过嵌入 javascript 来单击元素。通过使用 javascript 执行器,我们可以在驱动程序上运行 javascript。

WebElement element = driver.findElement(By.id("id-home_prevButton");
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

回答by user1419243

I also had a similar problem which was fixed by:

我也遇到了类似的问题,已通过以下方式修复:

String id = element.getAttribute("id");
((JavascriptExecutor)driver).executeScript("$('#id').click();");

回答by Tuan Le

I met the same issue. I captured the same element by using different Xpath and it worked. I also don't know why. Below is my case:

我遇到了同样的问题。我通过使用不同的 Xpath 捕获了相同的元素并且它起作用了。我也不知道为什么。下面是我的情况:

//a[contains(text(),'%s')]-> did not work.

//a[contains(text(),'%s')]-> 没有用。

//a//span[contains(text(),'%s')]-> worked.

//a//span[contains(text(),'%s')]-> 工作。

This is my HTML code:

这是我的 HTML 代码:

<a data-gtm-event="mbn-event-link" data-gtm-category="mbn-listing" data-gtm-action="home-category-l0-c3" href="/mua-ban-nha-dat-cho-thue-toan-quoc-l0-c3" title="B?t ??ng s?n ">
    <span class="clearfix">
        <span class="category-icon"><span class="icon icon-cat-3"></span></span>
        <span class="category-name">B?t ??ng s?n</span>
    </span>
</a>
<span class="clearfix">
    <span class="category-icon"><span class="icon icon-cat-3"></span></span>
    <span class="category-name">B?t ??ng s?n</span>
</span>

回答by user4955663

With Selenium Advanced User Interaction APImentioned e.g. here: I could fix this issue. Modified selenium code is as follows:

使用Selenium 高级用户交互 API,例如这里提到:我可以解决这个问题。修改后的selenium代码如下:

import org.openqa.selenium.support.ui.WebDriverWait
val waitPrevButton = new WebDriverWait(driver, 20)
val prevButton = driver.findElement(By.id("id-home_prevButton"))
log.debug(wEPrint("prevButton",prevButton))
waitPrevButton.until(ExpectedConditions.visibilityOfElementLocated(By.id("id-home_prevButton")))
log.debug("id-home_prevButton.click")
//prevButton.click()
//prevButton.sendKeys(Keys.RETURN)
//prevButton.sendKeys(Keys.ENTER)
new Actions(driver).moveToElement(prevButton).click().perform()

This fixes the issue but I would still like to know the reason for the ElementNotInteractableExceptionand why selenium complained "Element is not visible" after successfull visibilityOfElementLocatedtest. Any ideas?

这解决了问题,但我仍然想知道ElementNotInteractableExceptionselenium 在成功isibilityOfElementLocated测试后抱怨“元素不可见”的原因和原因。有任何想法吗?

回答by Nazia Sadiq

This issue will be resolve by simply using Implicit wait because web page loaded page completely then its find elements.

这个问题将通过简单地使用隐式等待来解决,因为网页加载页面完全然后它的查找元素。

driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); driver.get("URL");

driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); driver.get("URL");