Ivan Ivan - 19 days ago 6
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)
time.sleep(delay)
browser.save_screenshot(html_file + '.png')
browser.quit()


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?

Answer

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)
Comments