-1

I am trying to click on button which is not viewable on to the screen so that first I have to scroll the window and then perform click operation, but before scrolling entirely happens using JavascriptExecutor, Selenium tries to click on button which leads to ElementClickInterceptedException.

WebElement submitButton = driver.findElement(By.cssSelector(".totalRow button"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", submitButton);
w.until(ExpectedConditions.elementToBeClickable(submitButton));
submitButton.click();

If I use Thread.sleep(5000) after scrolling this issue does not happen, but I do not want to use that. I tried using JavascriptExecutor and Action class that did not work either.

JeffC
  • 22,180
  • 5
  • 32
  • 55
Pooja
  • 1
  • 1
  • Please post the full error message when `ElementClickInterceptedException` is thrown. That will tell us which element is intercepting the click. – JeffC Mar 16 '23 at 20:12
  • _`ElementClickInterceptedException`_: Update the question with the complete error stacktrace. – undetected Selenium Mar 16 '23 at 20:28
  • if you don't HAVE to use selenium, I would recommand that you try playwright. Both are great, but I've had better results in general with it. just in case – Je Je Mar 16 '23 at 20:55

2 Answers2

1

I don't see any major issue in your code as such. The complete error stacktrace would have helped us to understand exactly which element intercepts the click throwing ElementClickInterceptedException.

However, a minor improvement would be to induce WebDriverWait for the visibilityOfElementLocated() before you attempt to scrollIntoView() as follows:

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".totalRow button"))));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector(".totalRow button"))).click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Selenium IS waiting for the element to be clickable but it is not aware that another element is blocking the click until that click is attempted. There are two solutions to this problem:

  1. You can use a JS click to click submitButton.

    WebElement submitButton = driver.findElement(By.cssSelector(".totalRow button"));
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView(true);", submitButton);
    w.until(ExpectedConditions.elementToBeClickable(submitButton));
    js.executeScript("arguments[0].click();", submitButton);
    

    If you are creating UI test scripts, don't use this. Using a JS click will allow the script to click elements that a normal user will not be able to click. If you are just doing webscraping, etc. where you aren't trying to simulate actual user behavior, this method is fine.

  2. Wait for the blocking element to be invisible. You'll know which element is blocking the click because the HTML of that element will be in the exception message.

    WebElement submitButton = driver.findElement(By.cssSelector(".totalRow button"));
    ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", submitButton);
    By blockingElementLocator = ...;
    w.until(ExpectedConditions.invisibilityOfElementLocated(blockingElementLocator));
    w.until(ExpectedConditions.elementToBeClickable(submitButton)).click();
    
JeffC
  • 22,180
  • 5
  • 32
  • 55