python自动化测试——截图(截取页面特定元素)

时间:2019-02-25 17:14:38   收藏:0   阅读:496
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.***.cn")

filename = "image.png"
#截屏
driver.save_screenshot(filename)

#需要截取的元素定位
element = driver.find_element(By.ID, "***")
#获取元素上下左右的位置
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(filename)
#截取对应位置
im = im.crop((left, top, right, bottom))
#保存覆盖原有截图
im.save(filename)

driver.quit()

 

原文:https://www.cnblogs.com/nicole-zhang/p/10431797.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!