Selenium Full page Screenshot with scroll, Missing Some part of Web page

91 views
Skip to first unread message

Shadat Khan

unread,
Nov 16, 2022, 6:43:25 AM11/16/22
to Selenium Users
Hi, 

I am using python selenium for automation, To capture the details of the full page, i have scrolled page using Python Pillow i have stitched images dimensions to  viewport and inner document width and height , scrolling using java script executor. Though i am able to run the Python selenium solution in interactive way but it is unable to capture the full page screenshot while running the task in the task scheduler. 

Code:

def save_screenshots_SK(driver,dir_path):

options.add_argument("--hide-scrollbars")
    options.add_argument("--start-maximized")
    options.add_argument("--start-fullscreen")
    total_width = driver.execute_script("return document.body.offsetWidth")
    total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
    viewport_width = driver.execute_script("return document.body.clientWidth")
    viewport_height = driver.execute_script("return window.innerHeight")
    #driver.set_window_size(1920, total_height)
    rectangles = []
    i = 0
    while i < total_height:
        ii = 0
        top_height = i + viewport_height
        if top_height > total_height:
            top_height = total_height
        while ii < total_width:
            top_width = ii + viewport_width
            if top_width > total_width:
                top_width = total_width
            rectangles.append((ii, i, top_width, top_height))
            ii = ii + viewport_width
        i = i + viewport_height
    stitched_image = Image.new('RGB', (total_width, total_height))
    previous = None
    part = 0
    time.sleep(1)
    for rectangle in rectangles:
        #driver.set_window_size(1920, total_height)
        if not previous is None:
            driver.execute_script("window.scrollTo({0}, {1})".format(rectangle[0], rectangle[1]))
        file_name = "part_{0}.png".format(part)
        driver.get_screenshot_as_file(file_name)
        screenshot =  Image.open(file_name)
        time.sleep(1)
        if rectangle[1] + viewport_height > total_height:
            offset = (rectangle[0], total_height - viewport_height)
        else:
            offset = (rectangle[0], rectangle[1])
        stitched_image.paste(screenshot, offset)
        del screenshot
        os.remove(file_name)
        part = part + 1
        previous = rectangle
    #stitched_image.resize((total_width, total_height))
    time.sleep(2)
    stitched_image.save(dir_path)

 
Reply all
Reply to author
Forward
0 new messages