Ivan
Ivan

Reputation: 20101

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?

Upvotes: 2

Views: 2320

Answers (1)

Florent B.
Florent B.

Reputation: 42518

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)

Upvotes: 6

Related Questions