python自動化測試——截圖(截取頁面特定元素)


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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM