PIL 图片处理实例------(云打码登录)

from PIL import Image
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
# PIL 属于Pillow模块下的一个模块, 主要是用来处理图片
# 使用selenium进行验证码图片的截图
# 在页面上通过xpath/cssselect 定位到验证码图片;
# 获取验证码图片在整个大图中的坐标
# 根据坐标从大图截取小图

driver = webdriver.Firefox()
driver.get('http://yundama.com/')

image = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id('verifyImg'))

driver.save_screenshot('page.png')
left = image.location['x']
top = image.location['y']

right = left + image.size['width']
bottom = top + image.size['height']
locations = (left, top, right, bottom)

img = Image.open('page.png')
img = img.crop(locations)
img.save('captcha.png')


username = driver.find_element_by_css_selector('#username')
password = driver.find_element_by_css_selector('#password')
vcode = driver.find_element_by_css_selector('#vcode')

username.send_keys('xiatian')
password.send_keys('050924cll')

captcha = input("验证码: ")

vcode.send_keys(captcha)

driver.find_element_by_css_selector('#div_login > input').click()

你可能感兴趣的:(PIL 图片处理实例------(云打码登录))