使用Selenium的WebDriver进行长截图

python 复制代码
from selenium import webdriver
from PIL import Image
from io import BytesIO
# 创建浏览器驱动
driver = webdriver.Chrome()

# 打开网页
driver.get("https://www.douban.com/")  # 替换为您要截图的网页URL
def get_long_shot(driver,table_element):
    # 获取页面的初始高度和宽度
    # 获取表格元素的位置和大小
    table_location = table_element.location
    table_size = table_element.size

    # 创建一个与表格相同大小的窗口,并移动到表格位置
    driver.set_window_size(table_size["width"], table_size["height"])
    driver.set_window_position(table_location["x"], table_location["y"])

    # 初始化一个空白长截图
    long_screenshot = Image.new('RGB', (table_size["width"], table_size["height"]))

    # 向下滚动以截取表格的所有内容
    scroll_position = 0
    while scroll_position < table_size["height"]:
        driver.execute_script(f"window.scrollTo(0, {scroll_position})")
        screenshot = Image.open(BytesIO(driver.get_screenshot_as_png()))
        long_screenshot.paste(screenshot, (0, scroll_position))
        scroll_position += screenshot.height

    # 向右滚动以截取表格的所有内容
    scroll_position = 0
    while scroll_position < table_size["width"]:
        driver.execute_script(f"window.scrollTo({scroll_position}, 0)")
        screenshot = Image.open(BytesIO(driver.get_screenshot_as_png()))
        long_screenshot.paste(screenshot, (scroll_position, 0))
        scroll_position += screenshot.width

    # 保存长截图
    long_screenshot.save("table_long_screenshot.png")
相关推荐
把头塞进显示器2 天前
电商平台-测试报告
python·selenium·pytest
霍格沃兹测试学院-小舟畅学3 天前
UI 测试的语义断言:Midscene aiAssert 比像素比对稳在哪、又会骗在哪
人工智能·测试工具
zuozewei3 天前
附录 A:主流工具对比选型表
人工智能·测试工具
我的xiaodoujiao3 天前
Django 基础知识详细图文教程 10-Django 模板引擎 3
后端·python·测试工具·django
霍格沃兹测试学院-小舟畅学3 天前
Google 开源 ARTEMIS:AI Agent 如何接管 Android 真机测试?
人工智能·测试工具
Liaiyang663 天前
# 自研 AST 容错初筛工具:横向评测 Django、PyTorch、TensorFlow 三大开源 Python 框架异常收容风险
python·测试工具·自动化·开源软件·代码规范·devops·代码复审
霍格沃兹测试学院-小舟畅学3 天前
我用 AI 把 Swagger 变成 500 条 pytest 用例,回归从 3 天到 3 小时
人工智能·测试工具
奔跑中的小象4 天前
GuideLLM 压测实战指南:给大模型推理服务做一次完整 SLA 评估
测试工具·guidellm
cpolar技术支持4 天前
本地 Playwright 测试报告怎么远程复盘?Trace Viewer 跑起来后,用 cpolar 分享失败现场
前端·自动化测试·测试工具·cpolar·playwright
测试狗科研平台5 天前
材料热传导能力表征——热导率计算方法与流程解析
科技·测试工具·材料工程