十二、Selenium获取验证码图片

先对整个页面截图,然后根据验证码对位置裁切对方式获取验证码图片
需要依赖的三方库: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()

参数解释
crop(left,top,right,bottom)
这几个参数都是相对于父控件来说的,以父控件的左上角为原点(0,0)设置控件的位置


image.png

https://blog.csdn.net/daoshen1314/article/details/90757757
Selenium 3+Python 3 自动化测试项目实战 从菜鸟到高手 田春成 李靖 /著

你可能感兴趣的:(十二、Selenium获取验证码图片)