When i set the ChromeDriver in headless mode it is unable to get the source of the page in any way. The getPageSource() is working when i set the driver in non-headless mode.
Here is my code:
String pageSource = "";
try {
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
System.setProperty("webdriver.chrome.silentOutput", "true");
System.setProperty("webdriver.chrome.logfile", "/dev/null");
java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
ChromeOptions chromeOptions = new ChromeOptions();
//chromeOptions.setBinary("/usr/bin/chromium");
//chromeOptions.addArguments("--headless");
chromeOptions.setHeadless(true);
chromeOptions.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
driver = new ChromeDriver(chromeOptions);
if(driver!=null) {
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body")));
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.get(link);
pageSource = driver.getPageSource();
driver.quit();
}
} catch(TimeoutException te) {
//te.printStackTrace();
} catch (UnhandledAlertException eae) {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("Alert data: " + alertText);
alert.accept();
} catch (NoAlertPresentException e) {
// e.printStackTrace();
}
} catch (WebDriverException wde) {
//wde.printStackTrace();
}
I have already tried to get the source with other ways like for example using JavaScript but result is same.