使用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")
相关推荐
99乘法口诀万物皆可变2 小时前
CANoe_TestModule截图功能TestReportAddWindowCapture
测试工具
99乘法口诀万物皆可变11 小时前
ODX相关基础知识普及
测试工具·测试用例
今天好像不上班12 小时前
软件验证与确认实验二-单元测试
测试工具·算法
wdxylb1 天前
Pyhton爬虫使用Selenium实现浏览器自动化操作抓取网页
爬虫·selenium·测试工具
码力码力我爱你2 天前
C++ WebDriver扩展
c++·selenium·测试工具
能摆一天是一天3 天前
Python 爬虫 根据ID获得UP视频信息
开发语言·爬虫·python·selenium
土小帽软件测试3 天前
fiddler抓包16_自动重定向(mock数据)
测试工具·fiddler·抓包·软件测试学习
Kakaxiii3 天前
UMLS的api调用(Postman)
人工智能·深度学习·测试工具·自然语言处理·postman
可遇_不可求4 天前
使用Charles进行Mock数据:高效提升前端交互测试效率
测试工具
NPE~4 天前
爬虫入门 & Selenium使用
爬虫·python·selenium·测试工具·xpath