使用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")
相关推荐
Wpa.wk6 小时前
接口测试 - 接口测试工具 Postman-基础使用
经验分享·测试工具·lua·postman
可可南木7 小时前
ICT测试日志 --4--日志记录的格式 中
功能测试·测试工具·pcb工艺
卓码软件测评8 小时前
第三方软件测评机构:【Gatling构建JSON请求体StringBody、ElFileBody和Pebble模板的使用】
测试工具·性能优化·json·测试用例
美团程序员8 小时前
软件测试面试题总结【含答案】
功能测试·测试工具·职场和发展
syt_biancheng12 小时前
大规模考试系统性能优化与风险评估
python·功能测试·selenium·性能优化·postman
卓码软件测评13 小时前
CMA/CNAS软件测评机构:【Gatling XPath检查:XPath语法在XML响应中的应用】
测试工具·单元测试·测试用例
苏生十一_Nojambot1 天前
Postman下载安装与使用汉化版教程
测试工具·postman
卓码软件测评1 天前
软件测试:如何在Postman中配置和自动化OAuth 2.0与JWT认证?
测试工具·单元测试·自动化·测试用例·postman·可用性测试
一念一花一世界1 天前
接口管理工具选型:PostMan与PostIn全面对比指南
测试工具·postman·接口管理工具
软件测试雪儿1 天前
Postman越来越难用了
软件测试·测试工具·jmeter·postman