Headless Chrome Feature hangs indefinitely when taking a screenshot after switching to new window

3,770 views
Skip to first unread message

ashley...@rocketmail.com

unread,
Jun 13, 2017, 10:07:52 AM6/13/17
to ChromeDriver Users
I am currently trying to use the new --headless feature that has recently been released in the beta version of chrome in my selenium tests. And a few of my tests have instances where a link is clicked and the new page opens up in a new tab. In my code, I have methods to take screenshots when a step (which is a method) passes or when a step fails. I have noticed that when selenium tries to take a screenshot after a new tab has been opened and the window focus has been switched to the newly opened tab, that the test hangs indefinitely until I force quit the browser. Note: I've tried updating my chromedriver to the latest available version (2.30) and I'm still seeing the same results. Also, this only occurs when I run the tests with the --headless flag. When I run in chromedriver without the --headless flag, everything works just fine.

Code to open chrome in headless:

public class ChromeDriverFactory {

public ThreadLocal<WebDriver> driver;
private Set<WebDriver> drivers = Collections.newSetFromMap(new ConcurrentHashMap<>());

public ChromeDriverFactory(Boolean isHeadless) throws IOException
{
System.setProperty("webdriver.chrome.driver", TestUtils.getRelativePath() + "/externalLibraries/browsers/chromedriver");
System.setProperty("java.awt.headless", Boolean.toString(isHeadless));
DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setJavascriptEnabled(true);
caps.setCapability("takesScreenshot", true);
ChromeOptions options = new ChromeOptions();
if (isHeadless)
{
options.addArguments("headless");
options.addArguments("disable-gpu");
//options.addArguments("no-sandbox");
}
options.addArguments("disable-extensions");
caps.setCapability(ChromeOptions.CAPABILITY, options);

driver = new InheritableThreadLocal<WebDriver>(){
@Override
protected ChromeDriver initialValue() {
ChromeDriver chromeDriver = new ChromeDriver(caps);
drivers.add(chromeDriver);
return chromeDriver;
}
};
}
}

Method I'm using to switch to a new window when clicking on a link:

public void switchToNewWindow()
{
try
{
for (String window : LocalDriverManager.getDriver().getWindowHandles())
{
LocalDriverManager.getDriver().switchTo().window(window);
}
}
catch (WebDriverException ex)
{
throw ex;
}
}

Code I am using to take a screencap:

WebDriver _driver = new Augmenter().augment(driver);
File source = ((TakesScreenshot)_driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File(screenshotPath), true);
Errors that appear in the console when I force quit the stalled test:

[223.906][SEVERE]: Unable to receive message from renderer [223.906][WARNING]: screenshot failed, retrying

Chrome version: 60.0.3112.24, Selenium Version: 3.4.0, Chromedriver version: 2.30, OS: Mac, OS version: Sierra 10.12.5

Steps to reproduce:

Open the browser in headless mode.
Execute some step (i.e clicking a link) that opens a page in a new tab.
Switch focus to the new page in the newly open tab via selenium.
Take a screenshot via selenium.

Expected Result: The screenshot should be taken of the newly opened tab.

Actual Result: The browser hangs indefinitely until manually force quit. And no screencap is taken. Note: This issue does not occur when the --headless flag is removed.

ashley...@rocketmail.com

unread,
Jun 13, 2017, 12:12:32 PM6/13/17
to ChromeDriver Users, ashley...@rocketmail.com
NOTE: screencaps ARE being taken in --headless mode, but they only get taken if selenium does NOT have to switch windows because of a newly opened tab. This issue ONLY occurs when switching to a newly opened tab.

cristian...@gmail.com

unread,
Jun 28, 2017, 11:08:46 AM6/28/17
to ChromeDriver Users, ashley...@rocketmail.com
I am getting the same issue, however if i try to inspect any of the elements on the page, they will all be `displayed?: false`

Info about my setup:
(Session info: headless chrome=60.0.3112.40)
(Driver info: chromedriver=2.30.477690 (c53f4ad87510ee97b5c3425a14c0e79780cdf262),platform=Mac OS X 10.11.6 x86_64)

gmani...@chromium.org

unread,
Jun 28, 2017, 2:28:27 PM6/28/17
to ChromeDriver Users, ashley...@rocketmail.com, cristian...@gmail.com
Hi Cristian,

This issue is being tracked at https://bugs.chromium.org/p/chromedriver/issues/detail?id=1845. Please follow it for further updates.

Regarding 'displayed?: false', this issue where elements are marked as ‘not displayed’ on Headless chrome is a known one. Please refer document at https://bugs.chromium.org/p/chromium/issues/detail?id=604324#c36

Thanks

dte...@gmail.com

unread,
Dec 21, 2017, 5:21:09 PM12/21/17
to ChromeDriver Users
Hi,
is this working? I am having the same issue with the tabs

Chrome Version 63.0.3239.108
Driver 2.33
OS Windows 10 and Mac OS 10

sarat...@gmail.com

unread,
Feb 5, 2019, 4:47:48 PM2/5/19
to ChromeDriver Users
1) Get the driver as singleton

@Singleton
class BrowserInstance {

ChromeDriver getDriver(){


ChromeOptions options = new ChromeOptions()

options.addArguments("--headless --disable-gpu")
return new ChromeDriver(options)
}
}
2) Use Close and quit in finally block

finally {
chromeDriver.close()
chromeDriver.quit()
}
Result: you will be using only one instance at time and if you see task manager you will not find chromedriver and chrome process hanging.

Reply all
Reply to author
Forward
0 new messages