使用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")
相关推荐
Hacker_Fuchen1 小时前
外包干了一个月,技术明显进步。。。。。
自动化测试·软件测试·测试工具·职场和发展
测试199813 小时前
Appium使用指南与自动化测试案例详解
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
程序员三藏1 天前
快速弄懂POM设计模式
自动化测试·软件测试·python·selenium·测试工具·设计模式·职场和发展
程序员三藏2 天前
使用Docker和Selenium构建自动化测试环境
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
让世界再也没有bug2 天前
JMeter与Postman的区别
测试工具·jmeter·postman
优测云服务平台2 天前
质效飞跃,优测金融数智质效解决方案全新升级!
测试工具·金融
newxtc2 天前
【河北政务服务网-注册_登录安全分析报告】
人工智能·selenium·安全·yolo·政务·安全爆破
minglie13 天前
Wireshark抓HTTPS协议包
网络·测试工具·wireshark
测试19983 天前
压力测试详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·压力测试