【截图】selenium自动通过浏览器截取指定元素div的图片

【截图】selenium自动通过浏览器截取指定元素div的图片

思路

截取完整网页截图

通过元素的坐标 截图到指定位置的图片

前提是已经获取到 driver 了

python 复制代码
    # 定位目标div
    target_div = driver.find_element(By.CLASS_NAME, 'headlines-right')

    # 获取div的位置和大小
    location = target_div.location
    size = target_div.size


    # driver.switch_to.alert.accept()
    full_screenshot_path = 'full_screenshot.png'
    driver.save_screenshot(full_screenshot_path)

    # 根据div的位置和大小裁剪图片

    # 根据div的位置和大小裁剪图片
    image = Image.open(full_screenshot_path)

    left = location['x']
    top = location['y']
    right = location['x'] + size['width']
    bottom = location['y'] + size['height']

    target_image = image.crop((left, top, right, bottom))
    target_image.save('div_screenshot.png')

    print("目标div的截图已保存为 div_screenshot.png")
相关推荐
其实防守也摸鱼3 小时前
sqlmap下载和安装保姆级教程(附安装包)
linux·运维·服务器·测试工具·渗透测试·攻防·护网行动
介一安全8 小时前
JADX与AI结合的实操指南:从工具配置到APK分析
人工智能·测试工具·安全性测试·jadx
AALoveTouch1 天前
某麦网抢票:基于Wireshark协议分析
网络·测试工具·wireshark
x***r1511 天前
Wireshark-4.4.2-x64安装步骤详解(附网络抓包与分析入门教程)
网络·测试工具·wireshark
Cd ...1 天前
RobotFramework Selenium与Browser常用关键字对比
自动化测试·selenium·robotframework·playwright·browser
LT10157974441 天前
2026年性能测试平台信创适配评测:国产化场景落地能力解析
测试工具·性能优化
爆更小哇1 天前
pytest集成Allure报告教程
python·测试工具·pytest·接口测试·allure
12亡灵归来342 天前
Postman高级用法:自动化测试与Mock
测试工具·lua·postman
MESMarketing2 天前
互动分享 | 软件工具的安全合规实践
功能测试·测试工具·matlab·ci/cd·autosar