0

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!

3
  • button/span is the xpath will be for a span that doesn't emit a click event. Probably just need to remove /span and you will have the actual button element. Commented Nov 21, 2019 at 5:38
  • @MattSizzle hmm, just tried that, didn't work. Still puts out the same error Commented Nov 21, 2019 at 5:47
  • @Ray Can you try with this xpath: //span[text()='Add new']//parent::button Commented Nov 21, 2019 at 10:38

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Try clicking via the JavaScript: JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element);
Stop... Do you have another webdriver actions that succeed? Have you provided a chromedriver binary and configured path to it?
yes, i am able to open the page, maximise it, enter user credentials, and log in to the application using chrome webdriver
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.