selenium 截屏

当前环境:

Windows 10 + Python 3.7 + selenium 3.141.0 + Google Chrome 115.0.5790.110 (64 位)

python 复制代码
from selenium import webdriver
import base64


if __name__ == '__main__':

    #
    driver = webdriver.Chrome()
    driver.get('https://www.baidu.com/')

    # 1.1 全屏截图方法
    driver.get_screenshot_as_file(r'.\1.png')

    # 1.2 全屏截图方法
    driver.save_screenshot(r'.\2.png')

    # 1.3 全屏截图方法
    a = driver.get_screenshot_as_png()
    with open(r".\3.png", mode="wb") as f:
        f.write(a)

    # 1.4 全屏截图方法
    b = driver.get_screenshot_as_base64()
    c = base64.b64decode(b)
    with open(r".\4.png", mode="wb") as f:
        f.write(c)

    # 1.5 通过元素截图方法
    a = driver.find_element_by_xpath('//*[@id="su"]')
    a.screenshot(r'.\5.png')

    driver.quit()

1.png 、2.png、3.png 、4.png 如下图(并没有地址栏的地址):

5.png 如下图:

参考:

Python+Selenium基础篇之16-如何截图并保存_selenium截图保存_韦爵社的博客-CSDN博客

Selenium之截图方式_get_screenshot_as_png_勤奋的鸟的博客-CSDN博客

相关推荐
Bellafu6662 小时前
selenium常用的等待有哪些?
python·selenium·测试工具
Bellafu6664 小时前
selenium定位元素失败,常见错误有哪些?
selenium·测试工具
傻啦嘿哟10 小时前
用Selenium模拟登录淘宝并采集商品信息:从基础到实战
selenium·测试工具
起个破名想半天了14 小时前
五秒盾解决方案之Selenium
selenium·cloudflare·反爬
robinspada14 小时前
用mitmproxy替代selenium-wire
selenium·测试工具·mitmproxy·selenium-wire
我的xiaodoujiao14 小时前
从 0 到 1 搭建完整 Python 语言 Web UI自动化测试学习系列 17--测试框架Pytest基础 1--介绍使用
python·学习·测试工具·pytest
Bellafu66614 小时前
selenium的css定位方式有哪些
css·selenium·tensorflow
Bellafu66614 小时前
selenium对每种前端控件的操作,python举例
前端·python·selenium
Bellafu66614 小时前
下载selenium-ide及使用
ide·selenium·测试工具
将车24414 小时前
自动化测试脚本环境搭建
python·测试工具·自动化