Selenium screenshot full screen

Selenium screenshot full screen

Selenium screenshot full screen

When using Selenium for automated testing, sometimes it is necessary to capture the entire content of the web page, but the screen capture function save_screenshot() that comes with Selenium can only capture part of the picture. There are also functions on the Internet that scroll the page first and then splicing, but there will be repeated parts. , and parameters need to be set according to the page. The following method avoids the above problems, write it down to avoid forgetting.

def save_fullscreenshot(driver,screen_shot_name):
    # We need the dimensions of the content
    page_rect = driver.execute_cdp_cmd('Page.getLayoutMetrics', {
    
    })

    # parameters needed for ful 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(screen_shot_name, "wb") as fh:
        fh.write(base64.urlsafe_b64decode(base_64_png['data']))

result


insert image description here

Guess you like

Origin blog.csdn.net/Dbojuedzw/article/details/124381029