先對整個頁面截圖,然后根據(jù)驗證碼對位置裁切對方式獲取驗證碼圖片
需要依賴的三方庫:pillow庫 pip install pillow
截圖方法:save_screenShot("")
位置裁切:crop(left,top,right,bottom)
import time
from PIL import Image
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://user.qunar.com/passport/login.jsp?")
driver.maximize_window()
time.sleep(2)
driver.find_element_by_css_selector('body > div.qn_page > div.qn_wrap.clearfix > div.qn_right > div:nth-child(1) > a').click()
time.sleep(1)
driver.save_screenshot("qunaer.png")
screenshotpng = driver.find_element_by_id("vcodeImg")
left = screenshotpng.location['x']
top = screenshotpng.location['y']
print(left)
print(top)
right = left + screenshotpng.size['width']
bottom = top + screenshotpng.size['height']
print(right)
print(bottom)
qunaerImage = Image.open("qunaer.png")
qunaerImage = qunaerImage.crop((left, top, right, bottom))
qunaerImage.save("verifyImage.png")
time.sleep(2)
driver.close()
參數(shù)解釋
crop(left,top,right,bottom)
這幾個參數(shù)都是相對于父控件來說的,以父控件的左上角為原點(0,0)設置控件的位置
https://blog.csdn.net/daoshen1314/article/details/90757757
Selenium 3+Python 3 自動化測試項目實戰(zhàn) 從菜鳥到高手 田春成 李靖 /著