Невозможно нажать кнопку с помощью Selenium (Java, Chrome WebDriver) - PullRequest
0 голосов
/ 21 ноября 2019

Я довольно новичок в Selenium, и сталкиваюсь с некоторыми трудностями при нажатии кнопки с помощью Chrome WebDriver и xpath (а также при копировании FULL xpath). Я искал и читал другие сообщения, но все еще не уверен, что не так. Я пытался использовать функции By.className и By.cssSelector, но все еще не могу заставить его работать.

Также пытался заставить веб-драйвер ждать ...

Вот мой код (xpath извлечен путем проверки источника> проверяющий элемент> copy 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>

Кто-нибудь может мне помочь?

Спасибо взаранее!

1 Ответ

2 голосов
/ 21 ноября 2019

Во-первых, убедитесь, что элемент уже существует и находится в правильном состоянии, когда вы пытаетесь щелкнуть. Лучше всего подождать, пока он не станет кликабельным. Нравится

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