Element is not clickable at point ... error when using headless browsing

Viewed 923

I have this error "org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element is not clickable at point (209, 760)", when I run the piece of code below in headless mode. When it is run with browser displayed I have no error and test passes fine. As you can see below, I trie with waiting, js executor, actions move to element but still no good result. I am using xpath to locate / define the element, and not coordinates. Why is this happening please and how can I solve it? Thanks in advance.

@Test(priority = 1)
    public void verifyAddUserWithMarkedMandatoryFields() {

        // accessing add user webpage / functionality
        userListObject.getAddUserButton().click();

        // inserting data to complete form
        addOrEditUserPageObject.insertCredentials(userModel.getUsername(), userModel.getEmail(), "", userModel.getPassword());

        // clicking Submit when becoming enabled
        WebDriverWait myWaitVariable = new WebDriverWait(driver, 5);
              myWaitVariable.until(ExpectedConditions.elementToBeClickable(addOrEditUserPageObject.getSubmitButtonAddOrEdit()));

//            Actions actions = new Actions(driver);
//            actions.moveToElement(addOrEditUserPageObject.getSubmitButtonAddOrEdit()).click().perform();

            JavascriptExecutor jse = (JavascriptExecutor)driver;

//            jse.executeScript("scroll(209, 760)"); // if the element is on top.

            jse.executeScript("scroll(760, 209)"); // if the element is on bottom.

            addOrEditUserPageObject.getSubmitButtonAddOrEdit().click();
            
    }
1 Answers

You should add screen size for the headless mode, something like this:

Map<String,String> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadsPath); // Bypass default download directory in Chrome
prefs.put("safebrowsing.enabled", "false"); // Bypass warning message, keep file anyway (for .exe, .jar, etc.)
ChromeOptions opts = new ChromeOptions();
opts.setExperimentalOption("prefs", prefs);
opts.addArguments("--headless", "--disable-gpu", "--window-size=1920,1080","--ignore-certificate-errors","--no-sandbox", "--disable-dev-shm-usage");

driver = new ChromeDriver(opts);

I put much more things here, the only relevant point here is "--window-size=1920,1080", this should resolve your problem.
The rest is to show how things are managed, including other relevant settings for headless mode.

Related