Search code examples
pythongoogle-chromeselenium-webdrivergoogle-chrome-headless

I want to take a screenshot of the size specified by window size in headless(new) mode of selenuim


I am currently developing with python, selenium and chrome in headless(new) mode.
Now I want to take a screenshot of a specific size by specifying chrome's window-size.
However, when I actually take the screenshot, it saves an image of a different size.

The source code is written below.

options = Options()
options.add_experimental_option("excludeSwitches", ['enable-automation'])
options.add_argument('--headless=new')
options.add_argument("--window-size=800,600")
options.add_argument("--start-fullscreen")
driver = webdriver.Chrome(options=options)
driver.get(f"https://www.google.com/")
sleep(1)
print(driver.get_window_size()) #{'width': 800, 'height': 600}
driver.get_screenshot_as_file("./tmp.png")

The size of this tmp.png will be 1574 x 1188.
Why the difference in size? And how can I get a screenshot of the size I want?


Solution

  • I haven't tried to take ss with using selenium. However, you can use this Pillow to resize your image, and I hope it can be helpful.

    from PIL import Image
    image = Image.open('asdas.JPG')
    new_image = image.resize((500, 500))
    new_image.save('myimage_500.jpg')