Ivan Ivan - 4 months ago 36
Python Question

Selenium webdriver screenshot in Python has the wrong resolution

I´m taking screenshots using the selenium and Firefox geckodriver on Windows 10 with

delay = 5
browser = webdriver.Firefox(executable_path="C:\\Users\\A0048436\\Downloads\\geckodriver.exe")
browser.set_window_size(1920, 1080)
browser.get('file://' + html_file)
browser.save_screenshot(html_file + '.png')

so I expected that the image resolution would be the window size, but it´s not - it´s lower. How can I set the screenshot resolution?


The method set_window_size set the size of the window which includes the borders, menu bars and tabs.

To set the size of the viewport, you first need to compute the difference between the outer window and the inner window. Then add this difference to the desired resolution:

dx, dy = browser.execute_script("var w=window; return [w.outerWidth - w.innerWidth, w.outerHeight - w.innerHeight];")
browser.set_window_size(1920 + dx, 1080 + dy)