【问题标题】:Unable to click button using Selenium (Java, Chrome WebDriver)无法使用 Selenium(Java、Chrome WebDriver)单击按钮
【发布时间】:2020-03-16 23:20:30
【问题描述】:

我是 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>

谁能帮帮我?

提前致谢!

【问题讨论】:

  • button/span 是 xpath 将用于不发出点击事件的跨度。可能只需要删除 /span 就可以拥有实际的按钮元素。
  • @MattSizzle 嗯,刚试过,没用。仍然出现同样的错误
  • @Ray 你可以试试这个 xpath://span[text()='Add new']//parent::button

标签: java testing selenium-chromedriver rpa


【解决方案1】:

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

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

【讨论】:

  • 尝试通过 JavaScript 点击:JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element);
  • 停止...您还有其他成功的 webdriver 操作吗?您是否提供了 chromedriver 二进制文件并配置了路径?
  • 是的,我可以打开页面,将其最大化,输入用户凭据,然后使用 chrome webdriver 登录应用程序
【解决方案2】:

您尝试过 Actions 吗? new Actions(driver).moveToElement(element).click().perform();

【讨论】:

    猜你喜欢
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2017-01-17
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    相关资源
    最近更新 更多