使用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")
相关推荐
lifewange9 小时前
Cypress是什么
测试工具
川石课堂软件测试10 小时前
涨薪技术|Prometheus使用Recoding Rules优化性能
功能测试·测试工具·jmeter·mysql·面试·单元测试·prometheus
UTP协同自动化测试11 小时前
用UTP标准版搭建物联网模组交联测试环境:APP + UART + I2C + GPIO + PWM
嵌入式硬件·物联网·测试工具
123过去12 小时前
rsmangler使用教程
linux·测试工具·安全
123过去15 小时前
wordlists使用教程
linux·网络·测试工具·安全
llilian_161 天前
信号失真度测试仪 自动失真测试仪 低失真度自动测量仪为各行业精准赋能 自动失真仪
网络·功能测试·单片机·测试工具
Saniffer_SH1 天前
【每日一题】一台可编程的PCIe 6.0主机 + 一套自动化CTS验证平台 + 一个轻量级链路分析系统
运维·服务器·测试工具·fpga开发·自动化·计算机外设·硬件架构
123过去1 天前
hashid使用教程
linux·网络·测试工具·安全
赵得C1 天前
页面抓取全流程踩坑指南(从报错卡死到完整抓取视频+批量优化)
selenium·网络爬虫·webdriver
123过去1 天前
fcrackzip使用教程
linux·网络·测试工具·安全