Home>
I want to take a screenshot of the whole page with multiple urls in Selenium

However, it should scroll to the bottom of the page, but the page is cut off in the middle like the image.

Below is the code

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#Headless driver launch
options = Options ()
options.add_argument ('--headless')
driver = webdriver.Chrome (executable_path ='/ Users/○○○○/Desktop/WebDriver/chromedriver', options = options)
urls = [
        ]

for url in urls:
    driver.get (url)
    time.sleep (3)
    page_height = driver.execute_script ("return document.body.scrollHeight;")
    driver.set_window_size (1300, page_height)
    url_name = url.lstrip ("https: //" "http: //"). replace ("/", "_"). replace ("?", "_")
    driver.save_screenshot ("/ Users/○○○○○/Desktop/WebDriver/image_" + str (url_name) + ".png")
driver.quit ()

How can I fix it to take a screenshot to the bottom?

  • Answer # 1

    Reference: Take a screenshot of the entire page with Selenium (Python)

    #-*-coding: utf-8-*-
    "" "
    This code provides the ability to take a screenshot of the entire page with Selenium WebDriver.
    "" "
    import io
    from time import sleep
    from typing import List, Tuple, Optional
    from PIL import Image
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    def get_full_screenshot_image (driver, reverse = False, drivers_contains_scrollbar = None):
        # type: (selenium.webdriver.remote.webdriver.WebDriver, bool, Optional [bool])->Image.Image
        "" "
        take full screenshot and get its Pillow instance
        : param driver: Selenium WebDriver
        : param reverse: Paste from bottom direction when combining images. The default is False.
        : param driverss_contains_scrollbar: Set to True if the screenshot taken by WebDriver contains a horizontal scroll bar. Default is determined automatically.
        "" "
        if driverss_contains_scrollbar is None:
            drivers_contains_scrollbar = isinstance (driver, webdriver.Chrome)
        # Scroll to the bottom of the page once
        driver.execute_script ("window.scrollTo (0, document.body.scrollHeight);")
        sleep (0.5)
        scroll_height, document_client_width, document_client_height, inner_width, inner_height = driver.execute_script ("return [document.body.scrollHeight, document.documentElement.clientWidth, document.documentElement.clientHeight, window.innerWidth, window.innerHeight]")
        streams_to_be_closed = [] # type: List [io.BytesIO]
        images = [] # type: List [Tuple [Image.Image, int]]
        try: try:
            # open
            for y_coord in range (0, scroll_height, document_client_height):
                driver.execute_script ("window.scrollTo (0, arguments [0]);", y_coord)
                stream = io.BytesIO (driver.get_screenshot_as_png ())
                streams_to_be_closed.append (stream)
                img = Image.open (stream)
                images.append ((img, min (y_coord, scroll_height --inner_height))) # Image, y_coord
            #load
            scale = float (img.size [0])/(inner_width if drivers_contains_scrollbar else document_client_width)
            img_dst = Image.new (mode ='RGBA', size = (int (document_client_width * scale), int (scroll_height * scale)))
            for img, y_coord in (reversed (images) if reverse else images):
                img_dst.paste (img, (0, int (y_coord * scale)))
            return img_dst
        finally:
            # close
            for stream in streams_to_be_closed:
                stream.close ()
            for img, y_coord in images:
                img.close ()options = Options ()
    options.add_argument ('--headless')
    driver = webdriver.Chrome (executable_path ='/ Users/○○○○/Desktop/WebDriver/chromedriver', options = options)
    urls = [
            ]
    for url in urls:
        driver.get (url)
        sleep (3)
        img = get_full_screenshot_image (driver)
        img_name = /Users/○○○○○/Desktop/WebDriver/image_"+ url.lstrip ("https: //" "http: //"). replace ("/", "_"). replace (" ? "," _ ") +" .png "
        img.save (img_name)


  • Answer # 2

    With the code that I can scroll and connect to the screenshots
    Because I could take a screenshot of the whole page until the end without cutting
    I will ask this question.
    Thank you for your answers.

    ]

    Below is the code

    from PIL import Image
    import io
    def save_fullscreenshot (driver, filename):
        "" "Capture a full-page screenshot using image stitching" ""
        orig_overflow = driver.execute_script ("return document.body.style.overflow;")
        driver.execute_script ("document.body.style.overflow ='hidden';") # scrollbar
        total_height = driver.execute_script ("return document.body.scrollHeight;")
        total_width = driver.execute_script ("return document.body.scrollWidth;")
        view_width = driver.execute_script ("return window.innerWidth;")
        view_height = driver.execute_script ("return window.innerHeight;")
        stitched_image = Image.new ("RGB", (total_width, total_height))
        scroll_height = 0
        while scroll_height<total_height:
            col_count = 0
            scroll_width = 0
            driver.execute_script ("window.scrollTo (% d,% d)"% (scroll_width, scroll_height))
            while scroll_width<total_width:
                if col_count>0:
                    driver.execute_script ("window.scrollBy ("+ str (view_width) + ", 0)")
                img = Image.open (io.BytesIO (driver.get_screenshot_as_png ()))
                if scroll_width + view_width>= total_width \
                   or scroll_height + view_height>= total_height: #need cropping
                    new_width = view_width
                    new_height = view_height
                    if scroll_width + view_width>= total_width:
                        new_width = total_width --scroll_width
                    if scroll_height + view_height>= total_height:new_height = total_height --scroll_height
                    stitched_image.paste (
                        img.crop ((view_width --new_width, view_height --new_height,
                                  view_width, view_height)),
                        (scroll_width, scroll_height)
                    )
                    scroll_width + = new_width
                else: # no cropping
                    stitched_image.paste (img, (scroll_width, scroll_height))
                    scroll_width + = view_width
                col_count + = 1
            scroll_height + = view_height
        driver.execute_script ("document.body.style.overflow ='" + orig_overflow + "';")
        stitched_image.save (filename)
        return True
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    driver = webdriver.Chrome (executable_path ='/ Users/○○○○/Desktop/WebDriver/chromedriver')
    urls = [
            ]
    for url in urls:
        driver.get (url)
        time.sleep (1)
        url_name = url.lstrip ("https: //" "http: //"). replace ("/", "_"). replace ("?", "_")
        save_fullscreenshot (driver, "/ Users/○○○○/Desktop/WebDriver /" + str (url_name) + ".png")
    driver.quit ()

  • Answer # 3

    Python: Take a screenshot of the entire web page with Selenium + Headless Chrome

  • Answer # 4

    With a code that you can scroll and take screenshots yourself
    I've taken a screenshot of the entire page to the end without breaking, so I'll ask this question.
    Thank you for your answers.

    Below is the code

    from PIL import Image
    import io
    def save_fullscreenshot (driver, filename):
        "" "Capture a full-page screenshot using image stitching" ""
        orig_overflow = driver.execute_script ("return document.body.style.overflow;")
        driver.execute_script ("document.body.style.overflow ='hidden';") # scrollbar
        total_height = driver.execute_script ("return document.body.scrollHeight;")
        total_width = driver.execute_script ("return document.body.scrollWidth;")
        view_width = driver.execute_script ("return window.innerWidth;")
        view_height = driver.execute_script ("return window.innerHeight;")
        stitched_image = Image.new ("RGB", (total_width, total_height))
        scroll_height = 0
        while scroll_height<total_height:col_count = 0
            scroll_width = 0
            driver.execute_script ("window.scrollTo (% d,% d)"% (scroll_width, scroll_height))
            while scroll_width<total_width:
                if col_count>0:
                    driver.execute_script ("window.scrollBy ("+ str (view_width) + ", 0)")
                img = Image.open (io.BytesIO (driver.get_screenshot_as_png ()))
                if scroll_width + view_width>= total_width \
                   or scroll_height + view_height>= total_height: #need cropping
                    new_width = view_width
                    new_height = view_height
                    if scroll_width + view_width>= total_width:
                        new_width = total_width --scroll_width
                    if scroll_height + view_height>= total_height:
                        new_height = total_height --scroll_height
                    stitched_image.paste (
                        img.crop ((view_width --new_width, view_height --new_height,
                                  view_width, view_height)),
                        (scroll_width, scroll_height)
                    )
                    scroll_width + = new_width
                else: # no cropping
                    stitched_image.paste (img, (scroll_width, scroll_height))
                    scroll_width + = view_width
                col_count + = 1
            scroll_height + = view_height
        driver.execute_script ("document.body.style.overflow ='" + orig_overflow + "';")
        stitched_image.save (filename)
        return True
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    driver = webdriver.Chrome (executable_path ='/ Users/○○○○/Desktop/WebDriver/chromedriver')
    urls = [
            ]
    for url in urls:
        driver.get (url)
        time.sleep (1)
        url_name = url.lstrip ("https: //" "http: //"). replace ("/", "_"). replace ("?", "_")
        save_fullscreenshot (driver, "/ Users/○○○○/Desktop/WebDriver /" + str (url_name) + ".png")
    driver.quit ()