revy
revy

Reputation: 4707

Selenium with Chrome driver taking screenshots at double resolution on retina display

I am using Selenium with Chrome driver to taking some website screenshots. I need the screenshots to be at very specific resolution (1024x768). I've noticed that although the browser is correctly set at this resolution, the screenshot on disk is saved at double resolution (2048x1536). I suspect this is due the retina resolution of the macbook where I am running the application (mid 2017 macbook pro). This is the code I am using:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

width = 1024
height = 768

chrome_options = Options()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--lang=en')
chrome_options.add_argument('--headless')
chrome_options.add_argument(f'window-size={width}x{height}')

driver = webdriver.Chrome(options=chrome_options)

url = 'https://google.com'
driver.get(url)
print('Window size', driver.get_window_size())  # Window size {'width': 1024, 'height': 768}

driver.save_screenshot('test.png')  # Image is saved at 2048x1536

Is there a way to prevent the screenshot to be taken at double resolution on retina?

Upvotes: 7

Views: 1574

Answers (1)

revy
revy

Reputation: 4707

Found a possible solution:

chrome_options.add_argument('--force-device-scale-factor=1')

Upvotes: 13

Related Questions