12306的自动化登录

处理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")

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itLaity

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值