【截图】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")
相关推荐
tomorrow.hello8 小时前
Java并发测试工具
java·开发语言·测试工具
王者鳜錸10 小时前
使用Selenium自动化获取抖音创作者平台视频数据
selenium·自动化·音视频
weixin_4567325910 小时前
tcpdump交叉编译
网络·测试工具·tcpdump
chao_78910 小时前
frame 与新窗口切换操作【selenium 】
前端·javascript·css·selenium·测试工具·自动化·html
sunshine__sun2 天前
web网页用postman调用接口测试(超简单方法)
测试工具·postman
一筐大白菜啊2 天前
【工具】什么软件识别重复数字?
测试工具
随便写个昵称2 天前
selenium中xpath的用法大全
selenium·测试工具
随便写个昵称2 天前
selenium中find_element()用法进行元素定位
selenium·测试工具
chao_7892 天前
Selenium 操作表单选择控件【selenium】
运维·python·selenium·测试工具·自动化