selenium python 指定元素截图

时间:2021-12-27 15:22:10

1. 代码

# coding:utf-8
# coding:cp936
from selenium import webdriver
from PIL import Image

broswer = webdriver.Chrome()
broswer.get("http://www.baidu.com")
broswer.save_screenshot(r'E:\photo.png')
baidu = broswer.find_element_by_id('su')
left = baidu.location['x']
top = baidu.location['y']
elementWidth = baidu.location['x'] + baidu.size['width']
elementHeight = baidu.location['y'] + baidu.size['height']
picture = Image.open(r'E:\photo.png')
picture = picture.crop((left, top, elementWidth, elementHeight))
picture.save(r'E:\photo2.png')

2. 截图

selenium python 指定元素截图

截图 “百度一下”
selenium python 指定元素截图

参考:
1.利用 Python + Selenium 实现对页面的指定元素截图(可截长图元素)