原理:
1.截图(整个窗口)
2.获取此元素坐标
element = driver.find_element_by_id("xx")
element.location)
3.获取此元素大小
element = driver.find_element_by_id("xx")
element.size
4.根据元素坐标和元素大小确定此元素四个角坐标
5.依赖pillow,根据四角坐标提取图片并保存
案例源码:
首先pip安装pillow
代码语言:javascript复制pip install pillow
截图:
代码语言:javascript复制# coding:utf-8
from selenium import webdriver
from PIL import Image
driver = webdriver.Firefox()
driver.get('http://www.baidu.com/')
driver.save_screenshot('bdbutton.png')
element = driver.find_element_by_id("su")
print(element.location) # 打印元素坐标
print(element.size) # 打印元素大小
left = element.location['x']
top = element.location['y']
right = element.location['x'] element.size['width']
bottom = element.location['y'] element.size['height']
im = Image.open('bdbutton.png')
im = im.crop((left, top, right, bottom))
im.save('bdbutton.png')