Python 中使用 Selenium WebDriver 截取部分截图

2022/4/30 11:42:36

本文主要是介绍Python 中使用 Selenium WebDriver 截取部分截图,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

from selenium import webdriver
from PIL import Image
from io import BytesIO

driver= webdriver.Chrome()
driver.get('你的网页URL')

##按照页面进行元素定位
element = driver.find_element_by_xpath('你的元素')
##获取你的坐标{X,Y}
location = element.location
##获取高度跟宽度
size = element.size

##保存整个页面的截图
png = driver.get_screenshot_as_png()

##使用PIL库在内存中打开图像
im = Image.open(BytesIO(png))

##根据实际情况进行算法
top = location['y']
left = location['x']
bottom = + size['height']
right = + size['width']

im = im.crop((left, top, right, bottom))
##截图
im.save('截图.png')

driver.quit()



这篇关于Python 中使用 Selenium WebDriver 截取部分截图的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程