Bastian
Bastian

Reputation: 1237

org.openqa.selenium.ElementClickInterceptedException: element click intercepted error using Selenium and Java in headless mode

I have a project that I am working on with java and selenium. the test work OK in UI mode. However in headless mode I get this error

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>

how can I resolve this issue (working in UI mode). this is my code

WebDriver driver = getWebDriver();
        WebElement element;
        Thread.sleep(60000);
        element = driver.findElement(By.xpath("//label[@formcontrolname='reportingDealPermission']"));
        element.click();

why in selenium there is no operation to move to the element and break all layers. this is the UI. this is working in UI mode not working in headless mode, made sleep for 6 minutes and not resolved so this is not time issue

enter image description here

Upvotes: 16

Views: 144260

Answers (10)

Hameed Hasan H
Hameed Hasan H

Reputation: 1

In my case, I used double click functionality in Actions Worked. I tried all the other options but, I don't but for some reason this .doubleclick() function have worked. I just wanted to give it a try, it worked.

    WebElement checkbox = driver.findElement(By.id("mat-mdc-checkbox-1-input"));
    Actions action = new Actions(driver);
    action.moveToElement(checkbox).doubleClick().perform();

Upvotes: 0

Karansing
Karansing

Reputation: 45

If still facing issue then try this, clicking using JSExecutor worked for me:

((JavascriptExecutor) driver).executeScript("arguments[0].click();", <web_element_object>);

Upvotes: 0

inderjeet kumar
inderjeet kumar

Reputation: 11

Answer is worked for me.

Please check the below sreenshot for the my problem reference. Below alert is comes in between the the my button and frame. enter image description here

Upvotes: 0

Peraveen Kumar
Peraveen Kumar

Reputation: 1

In my case, nothing worked. Only the THREAD.SLEEP() method worked!

Upvotes: -1

Kondiram Kamble
Kondiram Kamble

Reputation: 11

WebDriverWait wait = new WebDriverWait(driver, 10); ---> has deprecated and it gives an error. Please use below instead:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@formcontrolname='reportingDealPermission']"))).click();

Ensure to import relevant items for the WebDriverWait and ExpectedConditions.

Upvotes: 1

Alexis Gamarra
Alexis Gamarra

Reputation: 4422

In my case "JavaScript" works:

WebElement ele = driver.findElement(By.xpath("(//input[@name='btnK'])[2]"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click()", ele);

It was taken from: http://makeseleniumeasy.com/2020/05/25/elementclickinterceptedexception-element-click-intercepted-not-clickable-at-point-other-element-would-receive-the-click/

Upvotes: 0

sagarp
sagarp

Reputation: 81

None of the above answers worked for me. Try using action class as follows:

WebElement element = driver.findElement(By.xpath("//div[@class='footer']"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();

Upvotes: 7

Ajit kumar Sahoo
Ajit kumar Sahoo

Reputation: 11

For this issue:

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562).

Another element receives the click:

Answer is to explicitly wait with javascript executor. This combination is working for me:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@formcontrolname='reportingDealPermission']"))); 
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193108

This error message...

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>

...implies that the click on the desired element was intercepted by some other element.


Clicking an element

Ideally, while invoking click() on any element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
    
  • xpath:

    new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@formcontrolname='reportingDealPermission' and @ng-reflect-name='reportingDealPermission']"))).click();
    

Update

After changing to headless if it still doesn't works and still get exception there still a couple of other measures to consider as follows:

  • Chrome browser in Headless mode doesn't opens in maximized mode. So you have to use either of the following commands/arguments to maximize the headless browser Viewport:

    • Adding the argument start-maximized

      ChromeOptions options = new ChromeOptions();
      options.addArguments("--headless");
      options.addArguments("start-maximized");
      WebDriver driver = new ChromeDriver(options);
      
    • Adding the argument --window-size

      ChromeOptions options = new ChromeOptions();
      options.addArguments("--headless");
      options.addArguments("--window-size=1400,600");
      WebDriver driver = new ChromeDriver(options);
      
    • Using setSize()

      ChromeOptions options = new ChromeOptions();
      options.addArguments("--headless");
      WebDriver driver = new ChromeDriver(options);
      driver.manage().window().setSize(new Dimension(1440, 900));
      

You can find a detailed discussion in Not able to maximize Chrome Window in headless mode

  • Additionally, you can also wait for the intercept element to be invisible using the ExpectedConditions invisibilityOfElementLocated before attempting the click() as follows:

    • cssSelector:

      new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.footer")));
      new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
      
    • xpath:

      new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@class='footer']")));
      new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@formcontrolname='reportingDealPermission' and @ng-reflect-name='reportingDealPermission']"))).click();
      

References

You can find a couple of related relevant discussions in:

Upvotes: 22

Wilfred Clement
Wilfred Clement

Reputation: 2774

Try adding an explicit wait

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@formcontrolname='reportingDealPermission']"))).click();

and if this doesn't work then try using the JS Executor

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@formcontrolname='reportingDealPermission']"))); 
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

Upvotes: 11

Related Questions