使用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")
相关推荐
要一杯卡布奇诺3 小时前
测开百日计划——Day1
功能测试·测试工具·单元测试·集成测试
安冬的码畜日常21 小时前
【JUnit实战3_28】第十七章:用 JUnit 5 实测 SpringBoot 项目
spring boot·功能测试·测试工具·设计模式·单元测试·junit5
程序员三藏1 天前
接口自动化测试框架搭建详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
卖个几把萌1 天前
【02】JMeter登录保存cookie用于后面的接口
测试工具·jmeter
测试老哥1 天前
Jmeter吞吐量控制器详解
自动化测试·软件测试·python·测试工具·jmeter·测试用例·压力测试
吗喽1号1 天前
python-xmind转Excel
python·测试工具
卓码软件测评1 天前
第三方软件测试机构:【“Bug预防”比“Bug发现”更有价值:如何建立缺陷根因分析与流转机制?】
功能测试·测试工具·单元测试·测试用例·压力测试·可用性测试
测试老哥2 天前
python+requests+excel 接口测试
自动化测试·软件测试·python·测试工具·测试用例·excel·接口测试
Run Freely9372 天前
web自动化测试_selenium_05_窗口截图、验证码处理
selenium·测试工具
Run Freely9372 天前
web自动化测试-Selenium04_iframe切换、窗口切换
selenium