Python爬虫-第五章-1-超级鹰插件实现自动填写识别码并登录12306网站

时间:2023-02-10 21:16:49

功能:自动打开浏览器,定位到网站登录界面,输入账户密码,填写识别码并登录到网站内部

# Demo Describe:12306登录案例

import time

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from chaojiying import Chaojiying_Client
from selenium.webdriver.common.action_chains import ActionChains # 事件链
from selenium.webdriver.chrome.options import Options

# chaojiying = Chaojiying_Client('mooreyxia', 'mooreyxia123', '933415')

# ''' 序别识别到是自动化工具运行浏览器的处理方案
# 如果程序别识别到是自动化工具运行浏览器的处理方案
# 1.chrome版本小于88的化 启动浏览器时嵌入js代码,去掉webdriver标识【chrome正收到自动测试软件的控制】
# ps:其他浏览器也可用
# web = Chrome()
# web.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',{
# 'source':'''
# navigator.webdriver = undefined
# Object.defineProperty(navigator,'webdriver',{
# get:() => undefined
# })
# '''
# })
# web.get(url)
# 2.下面测试内容是新版本chrome
# '''
option = Options()
option.add_argument('--disable-blink-features=AutomationControlled')
option.add_experimental_option("detach", True) # 防止谷歌浏览器在测试驱动运行完毕后自动关闭

web = Chrome(options=option)
web.get('https://kyfw.12306.cn/otn/resources/login.html')
time.sleep(1)

web.find_element(By.XPATH, value='//*[@]/div[2]/div[2]/ul/li[1]/a')
time.sleep(2)

'''
网站验证码识别并点击
'''
# # 处理登录验证码
# verify_img_element = web.find_element(By.XPATH,value='')
# # 超级鹰识别验证码
# # 9004-坐标多选,返回1~4个坐标,如:x1,y1|x2,y2|x3,y3
# dic = chaojiying.PostPic(verify_img_element.screenshot_as_png,9004)
# result = dic['pic_str']
# rs_list = result.split('|')
# for rs in rs_list:
# p_temp = rs.split(',')
# x = int(p_temp[0])
# y = int(p_temp[1])
# # 鼠标移动到某个位置,点击
# # move_to_element_with_offset - 移动到某个元素然后进行偏移
# ActionChains(web).move_to_element_with_offset(verify_img_element,x,y).perform()

'''
登录
'''
web.find_element(By.XPATH, value='//*[@]').send_keys('xxxx') --> 用户名
web.find_element(By.XPATH, value='//*[@]').send_keys('xxxx') --> 密码
web.find_element(By.XPATH, value='//*[@]').click()
time.sleep(7)

'''
滑块验证-意在拦截自动软件,若要滑块有效,需要屏蔽掉自动软件标识
参考:本页面中【序别识别到是自动化工具运行浏览器的处理方案】
ps:程序运行不要切换窗口
'''
slide_btn = web.find_element(By.XPATH, value='//*[@]')
ActionChains(web).drag_and_drop_by_offset(slide_btn, 300, 0).perform()

我是moore,大家一起加油!