How to make sure the dimensions of screenshot taken in headless mode Firefox driver are always same?

Viewed 299

I want to take screenshot of a local html page in full-screen. But the following code produces different dimensional files(screenshots) for different html files. I find it strange as I am doing it full-screen, It should not happen. Please help.

firefox_options = Options()
firefox_options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=firefox_options)
driver.get('file:///'+q.html.path)
driver.fullscreen_window()
shot = driver.get_screenshot_as_png()
driver.close()
2 Answers

Just add an option right after headless to set the window size:

firefox_options.add_argument("--window-size=1920x1080")

The driver.fullscreen_window() line isn't necessary for headless mode.

If I'm not mistaken when you use Options() you should go with it all the way!

So in your case, I would do this:

firefox_options = Options()
firefox_options.add_argument("--headless")
firefox_options.add_argument('--start-maximized')
driver = webdriver.Firefox(firefox_options=firefox_options)
driver.get('file:///'+q.html.path)
shot = driver.get_screenshot_as_png()
driver.close()

Hope this helps!

Related