# 导入webdriver import os import time import re import os from lxml import etree from selenium import webdriver from PIL import Image, ImageEnhance import pytesseract from func import base642str, str2base64 # 当前目录设置为根路径 ROOT_PATH = os.getcwd() # print(os.path.join(ROOT_PATH,‘1.txt‘)) # 将mm隐匿化 # import sys # # print(str2base64("123456")) # # str1_base64="MTIzNDU2" # # print(base642str(str1_base64)) # sys.exit() print(‘000-正在启用selenium...‘) # 调用环境变量指定的PhantomJS浏览器创建浏览器对象 chromedriver_exe_path = os.path.join(ROOT_PATH, "chromedriver.exe") driver = webdriver.Chrome(chromedriver_exe_path) print(‘000-OK‘) # url = ‘https://passport.baidu.com/v2/?login‘ print(‘111-selenium正在请求页面:%s‘ % url) driver.get(url) # get方法请求页面,获取响应 print(‘111-请求OK‘) print("打印标题") print(driver.title) # 点击账号和密码登录 input_box0 = driver.find_element_by_xpath("//p[@id=‘TANGRAM__PSP_3__footerULoginBtn‘]") input_box0.click() print(‘222-selenium正在填写表单...‘) time.sleep(1) account = "abcdef" pwd = "MTIzNDU2" verify_code = "1234" input_box1 = driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_3__userName‘]") input_box1.send_keys(account) time.sleep(0.5) input_box2 = driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_3__password‘]") input_box2.send_keys(base642str(pwd)) time.sleep(1.5) # 截图或验证码图片保存地址 screenImg = os.path.join(ROOT_PATH, "screenImg.png") # 判断是否需要验证码 imgsrc = driver.find_element_by_id("TANGRAM__PSP_3__verifyCodeImg").get_attribute(‘src‘) if re.match(r‘https://passport.baidu.com/cgi-bin/genimage.*‘, imgsrc): print("需要验证码") # 自动填写验证码 # input_box3 = driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_3__verifyCode‘]") # input_box3.send_keys(verify_code) # 浏览器页面截屏 driver.get_screenshot_as_file(screenImg) # 定位验证码位置及大小 location = driver.find_element_by_id(‘TANGRAM__PSP_3__verifyCodeImg‘).location size = driver.find_element_by_id(‘TANGRAM__PSP_3__verifyCodeImg‘).size left = location[‘x‘] top = location[‘y‘] right = location[‘x‘] + size[‘width‘] bottom = location[‘y‘] + size[‘height‘] # 从文件读取截图,截取验证码位置再次保存 img = Image.open(screenImg).crop((left, top, right, bottom)) img = img.convert(‘L‘) # 转换模式:L | RGB img = ImageEnhance.Contrast(img) # 增强对比度 img = img.enhance(2.0) # 增加饱和度 img.save(screenImg) print("图片验证码以保存:%s" % screenImg) # time.sleep(100) # 再次读取识别验证码 print("开始读取识别图片验证码") # screenImg=os.path.join(ROOT_PATH, "1.jpg") print(screenImg) img = Image.open(screenImg) code = pytesseract.image_to_string(img) print("识别结果:%s" % code) # time.sleep(100) # code= pytesser.image_file_to_string(screenImg) driver.find_element_by_id("TANGRAM__PSP_3__verifyCode").send_keys(code.strip()) print(code.strip()) time.sleep(2) # 手动填写验证码 # man_verify_code = input("请填写验证码:") # input_box3 = driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_3__verifyCode‘]") # input_box3.send_keys(man_verify_code) # print("验证码填写完成请等待") else: print("不需要验证码") print(‘222-填写表单OK‘) time.sleep(1) print(‘333-selenium提交表单...‘) one_click = driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_3__submit‘]") one_click.click() time.sleep(2) # 处理弹窗(手机验证码):点击关闭按钮 i = 0 while True: # 处理手机验证码认证反复弹窗 try: one3_click = driver.find_element_by_id("TANGRAM__%s__header_a" % (22 + i)) # 22+i在应对弹窗的关闭按钮id名称发生变化。 except Exception as e: print("无需手机验证码") break else: print("第 %s 次弹出安全验证,要求获取手机验证码" % (i + 1)) time.sleep(0.5) print("1s后自动选择无需手机验证码") time.sleep(1) one3_click.click() print("1s后自动点击登陆") time.sleep(1) one_click.click() time.sleep(2) # 判断是否成功登陆 current_title = driver.title.strip() if current_title.startswith("登录"): print(‘333-登陆失败...‘) time.sleep(0.5) print(‘333-2秒后自动重试...‘) i = i + 1 time.sleep(2) continue else: print("打印标题") print(driver.title) print(‘333-登录成功...‘) import sys sys.exit() # 处理弹窗(绑定手机号确认):点击不需要修改 # 判断是否需要手机号绑定确认 try: # 绑定手机号确认 one2_click = driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_27__rebindGuideCancel‘]") except Exception as e: print("无需绑定手机号确认") else: print("弹出了绑定手机号确认,1s后自动选择不需要") time.sleep(1) one2_click.click() # 打印标题 print("打印标题") print(driver.title) time.sleep(2) # 判断是否成功登陆 current_title = driver.title.strip() if current_title.startswith("登录"): print(‘333-登陆失败...‘) else: print(‘333-登录成功...‘)
原文地址:https://www.cnblogs.com/andy9468/p/10907741.html
时间: 2024-10-07 00:52:16