How to take full page screenshot of a webpage in a browser, below the visible screen, using python
Asked Answered
R

2

1

I am trying to open specific web pages and then take a full page screenshot of the webpage. I know this can be done using the dev tools in Chrome but I have not been able to find a method to do this using python. Does anyone know of any packages or methods to do this?

I have tried using Selenium to open the browser and webpage which was successful but the screenshot method only takes a screenshot of the visible screen and not the full page below the fold.

Retsina answered 28/10, 2023 at 21:5 Comment(0)
M
0

Yes, more modern web automation or test frameworks tend to have this functionality, e.g.:

And this answer from 2017 states that Selenium could not create those at the time. But apparently, there are workarounds (2022).

Marmara answered 28/10, 2023 at 21:37 Comment(0)
K
0

I found this reference that works very well with Chrome

https://dev.to/gdledsan/selenium-4-and-chrome-driver-take-full-page-screenthos-2j8d

from selenium import webdriver
import base64

driver = webdriver.Chrome()
driver.maximize_window()

driver.get("https://www.tutorialspoint.com/index.htm")

#get window size
page_rect = driver.execute_cdp_cmd('Page.getLayoutMetrics', {})

# parameters needed for full page screenshot
# note we are setting the width and height of the viewport to screenshot, same as the site's content size
screenshot_config = {
    'captureBeyondViewport': True,
    'fromSurface': True,
    'clip': {
        'width': page_rect['contentSize']['width'],
        'height': page_rect['contentSize']['height'],
         'x': 0,
         'y': 0,
         'scale': 1
    }
}
# Dictionary with 1 key: data
base_64_png = driver.execute_cdp_cmd('Page.captureScreenshot', screenshot_config)

# Write img to file
with open("imageToSave.png", "wb") as fh:
    fh.write(base64.urlsafe_b64decode(base_64_png['data']))
                                                  
driver.quit()

As for Firefox, it is much easier to get full page screenshots

driver = webdriver.Firefox()
driver.maximize_window()
driver.get("https://www.tutorialspoint.com/index.htm")
driver.save_full_page_screenshot("imageToSave.png")
Killam answered 29/10, 2023 at 0:47 Comment(1)
This worked perfectly! Thank you!Retsina

© 2022 - 2024 — McMap. All rights reserved.