使用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.wk2 小时前
自动化测试环境配置-java+python
java·开发语言·python·测试工具·自动化
程序员三藏6 小时前
Jmeter自动化测试
自动化测试·软件测试·python·测试工具·jmeter·测试用例·接口测试
西游音月12 小时前
(4)pytest+Selenium自动化测试-元素定位之CSS Selector定位
css·selenium·pytest
青青子衿_2121 小时前
TikTok爬取——视频、元数据、一级评论
爬虫·python·selenium
程序员小远2 天前
Appium-移动端自动测试框架详解
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
johnny2332 天前
AI加持测试工具汇总:Strix、
人工智能·测试工具
韩师学子--小倪2 天前
tcpdump + Wireshark:抓取nginx转发到服务的流量
测试工具·nginx·tcpdump
百花~2 天前
自动化测试概念篇~
selenium·log4j
一晌小贪欢2 天前
【Python办公】用 Selenium 自动化网页批量录入
开发语言·python·selenium·自动化·python3·python学习·网页自动化