【截图】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")
相关推荐
夜郎king4 小时前
Java搭配Selenium:实现网页访问与自动截图的实战指南及常见问题解析
selenium·java 网页截图·selenium截图实现
深蓝电商API5 小时前
Selenium 爬取 Canvas 渲染的数据图表
爬虫·python·selenium
可可南木6 小时前
3070文件格式--10--testorder文件格式详解
功能测试·测试工具·pcb工艺
深蓝电商API6 小时前
Selenium 动作链 ActionChains 高级用法
爬虫·python·selenium
我的xiaodoujiao7 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--Pytest框架钩子函数
python·学习·测试工具·pytest
可可南木1 天前
3070文件格式--9--boardxy文件格式详解 3
功能测试·测试工具·pcb工艺
不会代码的小测试1 天前
页面动态元素无法快速复制定位解决
python·selenium·自动化
安然无虞1 天前
「正则表达式」精讲
开发语言·测试工具·正则表达式
深蓝电商API1 天前
Selenium Grid分布式执行爬虫任务
爬虫·python·selenium
zbguolei1 天前
网络性能测试工具---iPerf
网络·测试工具