1

I am automating a website using selenium in Java.

<a id="pd-vote-button10359300" class="css-vote-button pds-vote-button"><span>Vote</span></a>

For this button I need to automate a Click in Selenium. I am doing by following but not working.

WebElement click = driver.findElement(By.id("pd-vote-button10359300"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", click);

Can you Please Suggest what is the Issue ?

After

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))).click();

I got Following Error

Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <span>...</span> is not clickable at point (122, 877). Other element would receive the click: <div class="nc_wrapper swp_floating_horizontal_wrapper bottom" style="background-color: rgb(255, 255, 255);">...</div>
 (Session info: chrome=77.0.3865.90)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'Sanjeevans-iMac.local', ip: '169.254.10.5', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.6', java.version: '13'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.90, chrome: {chromedriverVersion: 76.0.3809.126 (d80a294506b4..., userDataDir: /var/folders/k2/8cltlrwj23n...}, goog:chromeOptions: {debuggerAddress: localhost:59693}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 02cb57fb86be956e5e10be634b5724b1

    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.RemoteWebElement.execute(RemoteWebElement.java:285)

    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)

    at bigboss.A.main(A.java:49)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ruban4Axis
  • 821
  • 6
  • 27

4 Answers4

1

To click() on the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))).click();
    

Update

As an alternative you can use the executeScript() method as follows:

  • cssSelector:

    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.css-vote-button.pds-vote-button[id^='pd-vote-button']>span"))));
    
  • xpath:

    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button')]/span[text()='Vote']"))));
    

Note: As you are using java.version: '13' it is worth to mention that there are some compatibility issues between Selenium and / and you can find the detailed discussions in:


tl;dr

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

You are getting an ElementClickInterceptedException error, which means some other element on the page is overlapping the element you are trying to click. You will either need to interact with the page some how so the overlapping element no longer overlaps, or use JavaScript to both click on the element and trigger a "click" event.

Many web sites have page navigation elements that scroll along with the user, so something like a floating nav header might be obstructing the element you want to click on. DebanjanB has a good solution as a next step in troubleshooting this problem, but I suspect you will get a TimeoutException while waiting for the element to be clickable.

You will most likely need to watch this automated test execute and then play with the page after the test fails before you discover how to fix this.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
0

Why don't you do the following to click?

WebElement click = driver.findElement(By.id("pd-vote-button10359300"));
click.click()
karthikdivi
  • 3,466
  • 5
  • 27
  • 46
0

Possible your ID is getting change.Try below xpath.

//a[@class='css-vote-button pds-vote-button']/span[text()='Vote']

Code:

WebElement click = driver.findElement(By.xpath("//a[@class='css-vote-button pds-vote-button']/span[text()='Vote']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", click);
KunduK
  • 32,888
  • 5
  • 17
  • 41