Ray
Ray

Reputation: 43

Unable to click button using Selenium (Java, Chrome WebDriver)

I'm pretty new to Selenium, and am facing some difficulty clicking a button using Chrome WebDriver and xpath (and also by copying FULL xpath). I've searched and read the other posts, but still am not sure what's wrong. i've tried using the By.className and By.cssSelector functions, but still can't get it to work.

Also tried making the webdriver wait...

Here's my code (xpath extracted by inspecting source > inspecting element > 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();

and here is the error i'm getting

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)

I also tried using the javascript executor:

JavascriptExecutor executor = (JavascriptExecutor)webDriver;

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

which was followed by this error:

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)

Here is the source

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

Can anyone help me out?

Thanks in advance!

Upvotes: 0

Views: 338

Answers (2)

Raven
Raven

Reputation: 198

Have you try with Actions? new Actions(driver).moveToElement(element).click().perform();

Upvotes: 2

Eduard Dubilyer
Eduard Dubilyer

Reputation: 1013

First, make sure that element already exists and in the right state when you're trying to click. The best practice is to wait until it will be clickable. Like

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

Upvotes: 2

Related Questions