java - 无法使用 Selenium(Java、Chrome WebDriver)单击按钮

标签 java testing selenium-chromedriver rpa

我是 Selenium 的新手,在使用 Chrome WebDriver 和 xpath(以及通过复制完整的 xpath)单击按钮时遇到了一些困难。我已经搜索并阅读了其他帖子,但仍然不确定出了什么问题。我已经尝试使用 By.className 和 By.cssSelector 函数,但仍然无法正常工作。

还尝试让 webdriver 等待...

这是我的代码(通过检查源提取 xpath > 检查元素 > 复制 xpath):

WebDriverWait wait = new WebDriverWait(webDriver, 30);

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//[@id='contentPageWrapper']/div[2]/count-survey-component/div/div[3]/div/button/)")));

webDriver.findElement(By.xpath("//*[@id='contentPageWrapper']/div[2]/count-survey-component/div/div[3]/div/button/)")).click();

这是我得到的错误

Session ID: 7788e90631cad702049a4e60e946b7ae
*** Element info: {Using=xpath, value=//*[@id='contentPageWrapper']/div[2]/count-survey-component/div/div[3]/div/button/span)}
    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.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    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 EnterDataToWeb.enterDataToWeb(EnterDataToWeb.java:20)
    at Main.main(Main.java:103)

我也尝试过使用 javascript 执行器:

JavascriptExecutor executor = (JavascriptExecutor)webDriver;

executor.executeScript("arguments[0].click();","//[@id='contentPageWrapper']/div[2]/count-survey-component/div/div[3]/div/button/");

随后出现此错误:

Session ID: 0f0321ae1d4112a168ea588706f4f76c
    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.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    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.executeScript(RemoteWebDriver.java:489)
    at EnterDataToWeb.enterDataToWeb(EnterDataToWeb.java:31)
    at Main.main(Main.java:103)

这是来源

<button class="pull-right mat-blue mat-raised-button ng-star-inserted" mat-raised-button=$0>
<span class="mat-button-wrapper">Add new</span>
<div class="mat-button-ripple mat-ripple" matripple=""></div>
<div class="mat-button-focus-overlay"></div>
</button>

谁能帮帮我?

提前致谢!

最佳答案

首先,确保该元素已经存在并且在您尝试单击时处于正确的状态。最佳做法是等到它可以点击。喜欢

   new WebDriverWait(webdriver, 10)
      .until(ExpectedConditions.elementToBeClickable(element);
   element(click);

关于java - 无法使用 Selenium(Java、Chrome WebDriver)单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58967926/

相关文章:

python Selenium "Chrome is being controlled by automated test software"

python - Selenium/ChromeDriver 未知政策错误

python - 使用 selenium find_elements_by_xpath 多次返回同一元素的数组而不是所有元素

java - mybatis将时间戳转换为java日期是错误的

java - Java中的 map 内部 map

unit-testing - JEST 不会触发模拟函数 Vue js

android - 如何在android单元测试期间检查 Activity 是否显示对话框

Java 全局异常处理程序

java - 如何阻止输出空变量?

angularjs - 如何从 Protractor 中的 ng-repeat 获取值?