sokab
sokab

Reputation: 23

ChromeDriver with headless raises ElementNotVisibleException

I am new in selenium. I need a browser without a graphical interface because the project will start with Jenkins. I decided to use ChromeDriver in Headdless mode.

When I use ChrimeDriver in normal mode, I can click on all elements:

WebDriver driver = new ChromeDriver();
List<WebElement> allElem = driver.findElements(By.ByXPath("//div[@id='accordian']/div/ul/li"));

for(int i=0; i<allElem.getSize(); i++){
   allElem.get(i).click(); // is ok
}

But when I use Headdless mode then I have: ElementNotVisibleException: element not visible. What could be wrong? Thank you for every clue.

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--headless");
    //chromeOptions.addArguments("--start-maximized");
    WebDriver driver = new ChromeDriver(chromeOptions);
    List<WebElement> allElem = driver.findElements(By.ByXPath("//div[@id='accordian']/div/ul/li"));

    for(int i=0; i<allElem.getSize(); i++){
        allElem.get(i).click();//ElementNotVisibleException dont see next li elements 
        //div[@id='accordian']/div/ul/li
    }

Upvotes: 2

Views: 1705

Answers (2)

Du-Lacoste
Du-Lacoste

Reputation: 12817

You need to pass "--headless", chrome option like below.

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver =  new ChromeDriver(chromeOptions);

For entire list of chrome options, refer the following URL. It explains every command line switches in detail.

https://peter.sh/experiments/chromium-command-line-switches/

While working with headless mode, I encountered org.openqa.selenium.UnhandledAlertException due to not handling popping out of Alert Boxes. So it is better if you could handle the alert boxes.

                    String alertText = alert.getText();
                    System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
                    alert.accept();
                    File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                    String imageDetails = "D://Images"
                    File screenShot = new File(imageDetails).getAbsoluteFile();
                    FileUtils.copyFile(outputFile, screenShot);
                    System.out.println("Screenshot saved: {}" + imageDetails);
                    driver.close();

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193388

While working with Selenium Client v3.11.0, Chrome Driver v2.36 and Chrome Browser v65.x in Headless Mode, you need to pass the following arguments through an instance of ChromeOptions Class while initializing the WebDriver and the Web Browser as follows :

System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--disable-extensions"); 
WebDriver driver =  new ChromeDriver(chromeOptions);
driver.get("https://www.google.co.in");

Upvotes: 1

Related Questions