使用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")
相关推荐
whabc10017 小时前
ubuntu24.04安装selenium、chrome、chromedriver
chrome·selenium·测试工具
惜.己19 小时前
selenium获取元素 出现的错误AttributeError: ‘TestPage‘ object has no attribute ‘driver‘
python·selenium·pycharm
吃饱了想撑死1 天前
Postman四种请求教程
测试工具·postman
大白曾是少年2 天前
基于Postman进行http的请求和响应
测试工具·http·postman
成成成成成成果2 天前
揭秘动态测试:软件质量的实战防线
python·功能测试·测试工具·测试用例·可用性测试
卓码软件测评2 天前
软件测评中网站类测评测试使用的BurpSuite-Web安全测试流程
测试工具·安全·web安全
测试狗科研平台3 天前
TEM在量子点发光材料领域的应用-测试GO
测试工具·量子计算·材料工程
测试老哥3 天前
如何写好测试用例?
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
Dxy12393102163 天前
报错:selenium.common.exceptions.ElementNotInteractableException: Message
selenium·测试工具·microsoft
敲上瘾3 天前
渗透测试常用指令
服务器·测试工具·网络安全·压力测试