使用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")
相关推荐
随便写个昵称5 小时前
selenium中xpath的用法大全
selenium·测试工具
随便写个昵称7 小时前
selenium中find_element()用法进行元素定位
selenium·测试工具
chao_7897 小时前
Selenium 操作表单选择控件【selenium】
运维·python·selenium·测试工具·自动化
测试老哥12 小时前
Python+Selenium实现自动化测试
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
测试老哥18 小时前
软件测试之单元测试
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
测试199820 小时前
软件测试之压力测试总结
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·压力测试
好奇的菜鸟1 天前
在 Postman 中高效生成随机环境变量的完整指南
测试工具·lua·postman
车载测试工程师1 天前
汽车功能安全-嵌入式软件测试(软件合格性测试)【目的、验证输入、集成&验证要求】11
功能测试·网络协议·测试工具·安全·车载系统·汽车·测试覆盖率
chao_7891 天前
CSS表达式——下篇【selenium】
css·python·selenium·算法
随便写个昵称1 天前
selenium跳转到新页面时如何进行定位
selenium·测试工具