使用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")
相关推荐
可可南木9 小时前
3070文件格式--9--boardxy文件格式详解 3
功能测试·测试工具·pcb工艺
不会代码的小测试11 小时前
页面动态元素无法快速复制定位解决
python·selenium·自动化
安然无虞15 小时前
「正则表达式」精讲
开发语言·测试工具·正则表达式
深蓝电商API16 小时前
Selenium Grid分布式执行爬虫任务
爬虫·python·selenium
zbguolei16 小时前
网络性能测试工具---iPerf
网络·测试工具
深蓝电商API17 小时前
Selenium结合Chrome DevTools协议加速爬取
爬虫·python·selenium·测试工具·chrome devtools
猿小羽1 天前
[TEST] Selenium 自动化测试 - 1769143497387
selenium·测试工具
深蓝电商API1 天前
Selenium多窗口切换与Cookie管理
爬虫·python·selenium·测试工具
猿小羽1 天前
[TEST] Selenium 自动化测试 - 1769133931910
selenium·测试工具
木叶子---1 天前
Postman测试WebService接口
测试工具·postman