1

I have this Java app that on Ubuntu, with Selenium and ChromeDriver, scraps some websites and retrieves some data. I wanted to automate error logging, including a screenshot being created if certain exceptions are thrown(all already implemented and working). Basically this is where I instantiate my driver:

System.setProperty("webdriver.chrome.driver", System.getProperty("user.home") + driversPath + "chromedriver");
ChromeOptions options = new ChromeOptions();
if(headless)
    options.addArguments("--headless");
options.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
options.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
this.driver = new ChromeDriver(options);
this.driver.manage().window().setSize(new Dimension(1366, 768));

And this is how I create screenshots from it:

File srcFile = ((TakesScreenshot) this.driver).getScreenshotAs(OutputType.FILE);
    try {
        // creates file at System.getProperty("user.dir")
        FileUtils.copyFile(srcFile, new File(path));
    //catch and so on

With Boolean headless = false; this works exactly as I wanted, and I can see my screenshots just fine. Now I would like to test in some VPS and needed to use it headless first locally.

With Boolean headless = true; I noticed my automation worked as supposed by the log that was generated but when I intentionally got offline to throw some errors, I noticed all screenshots were blank images. I looked here but since with headless turned off I got screenshots, this answer doesn't matter to me.

I tried using setHeadless(boolean headless) from ChromeOptions that has this code:

public ChromeOptions setHeadless(boolean headless) {
    args.remove("--headless");
    if (headless) {
        args.add("--headless");
        args.add("--disable-gpu");
    }
    return this;
}

But it didn't work either. I've found this question but since I already got how to retrieve some screenshots from 'headfull' browser I think this isn't my case either.

I'm using ChromeDriver from this link(2.42)(the most recent at the time this question was made) and this dependency:

<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.9.1</version>
</dependency>
j.barrio
  • 1,006
  • 1
  • 13
  • 37
rado
  • 5,720
  • 5
  • 29
  • 51
  • remove the headless option and try it again. Sometimes in headless browser screenshot is not taken. – Hiten Sep 20 '18 at 03:40
  • @Hiten Sorry if I wasn't clear, I'll edit the question. I want some screenshots **with** headless mode on – rado Sep 20 '18 at 03:51
  • Can you also add any exception your code returns? – BountyHunter Sep 20 '18 at 05:20
  • In some website If we ran headless browser, then it will not work. you can try with phantom js. – Hiten Sep 20 '18 at 12:59
  • @BountyHunter All expcetions thrown were made by me and were part of my business logic, they have nothing to do with technical/infrastructure about Selenium or ChromeDriver – rado Sep 20 '18 at 13:44
  • Can you update the question with the related code block where you get _blank images_? – undetected Selenium Sep 20 '18 at 16:26

0 Answers0