Selenium 截全屏

Selenium 截全屏

Selenium 截全屏

当使用Selenium进行自动化测试时,有的时候需要截取网页的全部内容,但是Selenium自带的截屏函数save_screenshot()只能截取部分图片,网上也有先滚动页面再拼接的函数,但是会有重复的部分,而且需要根据页面设置参数。下面这种方法避免了上述问题,写下来避免遗忘。

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']))

结果


在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值