#遇到的问题
在进行python进行爬虫学习的时候。使用超级鹰与selenium进行12306的登陆。代码运行到最后却显示验证码校验失败。无论是通过程序还是手动输入拖动都是这样。只有不通过selenium开启的浏览器访问才可以正常登陆。
附上代码
from selenium.webdriver import Chrome
from chaojiying import Chaojiying_Client
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import time
chaojiying = Chaojiying_Client('********', '*********', '921331') #超级鹰的账号与密码隐藏了
ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
option = Options()
option.add_experimental_option('useAutomationExtension', False)
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument('User-agent='+ua)
option.add_argument('--disable-blink-features=AutomationControlled')
web = Chrome(options=option)
script = 'Object.defineProperty(navigator,"webdriver",{get:()=>undefined,configurable: true});'
web.execute_script(script)
web.get('https://kyfw.12306.cn/otn/resources/login.html')
time.sleep(1)
web.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
# 用户中心>>软件ID 生成一个替换 96001
time.sleep(1)
web.find_element_by_xpath('//*[@id="J-userName"]').send_keys("************") #账号
web.find_element_by_xpath('//*[@id="J-password"]').send_keys("*************") #密码
verify_element = web.find_element_by_xpath('//*[@id="J-loginImg"]')
img = verify_element.screenshot_as_png
dic = chaojiying.PostPic(img, 9004)
loc_list = dic['pic_str'] # x1,y1|x2,y2
result = loc_list.split('|')
for rs in result:
p_temp = rs.split(",")
x = int(p_temp[0])
y = int(p_temp[1])
# 动作链,执行一系列动作
ActionChains(web).move_to_element_with_offset(verify_element, x, y).click().perform()
time.sleep(2)
web.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[2]/div[5]/a').click()
time.sleep(5)
btn = web.find_element_by_xpath('/html/body/div[5]/div[2]/div/div[3]/div/div[1]/span')
ActionChains(web).drag_and_drop_by_offset(btn, 300, 0).perform()