【截图】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")
相关推荐
为你奋斗!2 小时前
图形化界面工具 - webUI使用Page Assist 插件
测试工具
CN.LG2 小时前
抓包工具 Wireshark 是什么?
网络·测试工具·wireshark
星空4 小时前
postman复习
测试工具·postman
念越12 小时前
自动化测试入门指南:Selenium环境搭建+第一个实战案例
自动化测试·selenium·测试工具·蓝桥杯
站长工具箱12 小时前
鼠标在线测试工具:全维度检测鼠标性能,本地处理更安全
测试工具·计算机外设
老师好,我是刘同学12 小时前
Verdi波形调试全功能详解
测试工具
小邓睡不饱耶12 小时前
实战教程:基于Selenium+BeautifulSoup爬取易车网新能源汽车销量数据
selenium·测试工具·beautifulsoup
sheepfagdng12 小时前
Python-web自动化-selenium(2)
运维·selenium·自动化
虚幻如影15 小时前
Selenium 自动化测试中 Chrome 浏览器弹出“您的连接不是私密连接”
chrome·selenium·测试工具