#通过ActionChains+autoit进行验证码图片的下载,通过pytesseract识别验证码中的字符,由于有一定错误率,进行重试直至成功
from selenium import webdriver
from LanternAnswer.login import Login
# download.default_directory:设置下载路径
# profile.default_content_settings.popups:设置为 0 禁止弹出窗口
# chrome 60以上版本无效,暂未找到方法。
# options = webdriver.ChromeOptions()
# prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'd:\\'}
# options.add_experimental_option('prefs', prefs)
# driver=webdriver.Chrome(executable_path='D:\\Python\\chromedriver.exe',chrome_options=options)
driver = webdriver.Chrome(executable_path='D:\\Python\\chromedriver.exe')
print(type(driver))
driver.implicitly_wait(10)
driver.maximize_window()
# 直接访问登录界面
driver.get("https://sso.dtdjzx.gov.cn/sso/login")
# 通过首页进入登录界面
# driver.get("http://www.dtdjzx.gov.cn")
# driver.find_element_by_xpath("/html/body/div[4]/a/img").click()
# windows=driver.window_handles
# driver.switch_to.window(windows[-1])
# time.sleep(10)
# popup=driver.find_element_by_id("l_mods")
# popup.find_element_by_id("mkjh").click()
# target = driver.find_element_by_xpath("/html/body/div[5]/div[2]/div/div[3]")
# driver.execute_script("arguments[0].scrollIntoView();", target)
# # time.sleep(10)
# # pyautogui.click(200,200)
# time.sleep(10)
# target.find_element_by_id("lbuts").click()
# # driver.find_element_by_xpath('//*[@id="myxiaoxi"]')
# Select(driver.find_element_by_id("shenfen")).select_by_visible_text("党员")
# driver.find_element_by_xpath('//*[@id="bts"]').click();
login = Login();
login.getvalidateCode(driver);
login.login(driver, "hisense", "hisense")
while (driver.find_element_by_id("validateCodeMessage").text == "验证码错误" or driver.find_element_by_xpath('//*[@id="parsley-id-9"]/span').text =="请输入图片验证码"):
login.getvalidateCode(driver);
login.login(driver, "hisense", "hisense")
login.py
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import pytesseract
from PIL import Image
import win32api
import time
class Login():
def __init__(self):
self.validateCode="0000";
def login(self, driver,usrname,password):
driver.find_element_by_id("username").clear();
driver.find_element_by_id("username").send_keys(usrname);
driver.find_element_by_id("password").clear();
driver.find_element_by_id("password").send_keys(password);
driver.find_element_by_id("validateCode").clear();
driver.find_element_by_id("validateCode").send_keys(self.validateCode.strip());
driver.find_element_by_xpath('//*[@id="loginForm"]/div[4]/a[1]').click();
def getvalidateCode(self,driver):
# codeimgsrc=driver.find_element_by_id("yanzhengma").get_attribute("src");
# codeimgurl="https://sso.dtdjzx.gov.cn/sso/login/"+codeimgsrc
# 使用action进行右键操作弹出菜单,使用autoit进行windows保存操作
codeimg=driver.find_element_by_id("yanzhengma");
action=ActionChains(driver);
action.move_to_element(codeimg)
action.context_click(codeimg);
# action.send_keys(Keys.DOWN);
# action.send_keys("v")
action.perform()
win32api.ShellExecute(0,'open','D:\Python\saveimg.exe','','',1)
time.sleep(10)
image = Image.open("D:\\CodeImage.jpg")
image=image.convert('L')
#1.FileNotFoundError: [WinError 2] 系统找不到指定的文件
# 方法1[推荐]: 将tesseract.exe添加到环境变量PATH中方法
#2: 修改pytesseract.py文件,指定tesseract.exe安装路径
# 2.pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Tesseract-OCR\\tessdata/eng.traineddata')
# 方法:在.py文件配置中指定tessdata-dir
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
tessdata_dir_config = '--tessdata-dir "C:\\Program Files\\Tesseract-OCR\\tessdata"'
self.validateCode = pytesseract.image_to_string(image,config=tessdata_dir_config)
# self.validateCode=validateCode.strip()