处理12306登录:
from selenium.webdriver import Chrome
from chaojiying import Chaojiying_Client
from selenium.webdriver.common.action_chains import ActionChains # 事件链
from selenium.webdriver.chrome.options import Options
import time
#初始化 🦅
chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '916474')
#在 12306中我们的 selenium 会被对方的系统识别出来 所以:
#1.chrome 的版本号如果 小于88 : 在你启动浏览器的时候(此时没有加载任何网页内容),向页面嵌入js代码 ,去掉webdriver
#2.chrome的版本号 如果大于等于88 本人90
options = Options
options.add_argument("--disable-blink-features=AutomationControlled") #允许自动化操作
web = Chrome(options=options)
web.get('https://kyfw.12306.cn/otn/resources/login.html')
time.sleep(5)
web.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
web.find_element_by_xpath('//*[@id="J-userName"]').send_keys('WWSyyds')
web.find_element_by_xpath('//*[@id="J-password"]').send_keys('wang9264')
time.sleep(2)
#处理验证码:
img = web.find_element_by_xpath('//*[@id="J-loginImg"]')
dic = chaojiying.PostPic(img.screenshot_as_png, 9004)
V_code = dic['pic_str']
re_list = V_code.split('|')
# print(V_code)
for rs in re_list:
p_da = rs.split(',')
x = int(p_da[0])
y = int(p_da[1])
# 让鼠标移动到某个位置 ,然后进行点击
ActionChains(web).move_to_element_with_offset(img,x,y).click().perform() # 定义事件 .perform() 提交
# move_to_element_with_offset() 移动到一个节点 并且带着偏移量 节点 : img中的节点 上面已经切割了
# 登录
web.find_element_by_xpath('//*[@id="J-login"]').click()
time.sleep(5)
#拖拽:
# btn = web.find_element_by_xpath('拖动图案的xpath')
ActionChains(web).drag_and_drop_by_offset(btn , 300,0).perform()
关于被浏览器检测你使用自动化的嵌入代码:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = webdriver.Chrome(chrome_options=options)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
chrome_options.add_argument("--disable-blink-features=AutomationControlled")