macOS python3 selenium 驗證碼截圖
1,驗證碼截圖
#! /usr/local/bin/python3
# coding:utf-8
from selenium import webdriver
from PIL import Image
browser = webdriver.Chrome()
url = "https://www.baidu.com"
browser.get(url)
baidu = browser.find_element_by_id("su")
# 獲取元素位置location、大小size
print ("su元素位置 x y : " , baidu.location)
print ("su元素大小 高寬 : " , baidu.size)
# x 窗口左邊框到元素的距離
left = baidu.location['x']
print ("x : ",left)
# y 窗口上邊框到元素的距離
top = baidu.location['y']
print ("y : ",top)
right = left + baidu.size['width']
print("right : ",right)
bottom = top + baidu.size['height']
print("bottom : ",bottom)
# 網頁窗口全屏截圖
window = browser.save_screenshot("/root/baidu.png")
# 剪切出驗證碼圖片
im = Image.open('/root/baidu.png')
im = im.crop((left , top , right , bottom))
im.save("/root/code.png")
print ("測試")
browser.quit()
參考: