# -*- coding:utf-8 -*-
from selenium import webdriver
from PythonHTTP调用示例.YDMHTTP import YDMHttp
#引用显示等待
from selenium.webdriver.support.ui import WebDriverWait
from PIL import Image
driver = webdriver.Firefox()
driver.get("https://ics.autohome.com.cn/passport/account/login")
# 找到用户名输入
username = WebDriverWait(driver,10).until(lambda driver: driver.find_element_by_id("UserNameDealer"))
username.send_keys("元信汽车")
# 找到密码框输入
password = WebDriverWait(driver, 10).until(lambda driver:driver.find_element_by_id("PasswordDealer"))
password.send_keys("123456")
# 找到验证码标签
captcha = WebDriverWait(driver, 10).until(lambda driver:driver.find_element_by_id("imgValidCodeDealer"))
captcha.screenshot(filename="captcha.png")
# 截图保存的另一个函数
# driver.save_screenshot("page.png")
# left = captcha.location["x"]
# top = captcha.location["y"]
# right = captcha.location["x"] + captcha.size["width"]
# bottom = captcha.location["y"] + captcha.size["height"]
# #
#
# img = Image.open("page.png")
# img = img.crop((left,top,right,bottom))
# img.save("captcha.png")
ydm = YDMHttp()
ydm.login()
cid,result = ydm.decode("captcha.png",5000,10)
print(result)
driver.find_element_by_id("checkCodeDealer").send_keys(result)
WebDriverWait(driver,10).until(lambda driver:driver.find_element_by_id("btnDealer")).click()