Python使用selenium模块模拟登录12306

selenium模块概述

  selenium模块是基于浏览器自动化的一个模块。换句话说使用selenium可以让浏览器根据自己写的代码自动运行。

  相应的语法

  ·编写基于浏览器自动化的操作代码

  · 发起请求:get(url)

  · 标签定位:find系列的方法

  · 标签交互:send_ keys( 'xxx')

  · 执行js程序:excute _script( 'jsCode ')一前进,后退:back() ,forward()

  · 关闭浏览器:quit()

  这里就不赘述selenium模块的安装了。

  登录的具体流程

  总体思路:

  · 使用selenium向12306网站发起请求,打开登陆页面

  · 根据标签的id值定位到用户账号框、用户密码框以及登录按钮

  · 填入账号密码后,点击登录按钮

  · 进行滑块验证

  · 登陆成功

  # 根据id获取用户账号输入框、密码输入框

  username_tag = driver.find_element_by_id('J-userName')

  password_tag = driver.find_element_by_id('J-password')

  # 填入自己的账号和密码

  username_tag.send_keys('xxxxxxx')

  time.sleep(1)

  password_tag.send_keys('xxxxxxxx')

  # 根据id获取登录按钮

  login_btn = driver.find_element_by_id('J-login')

  # 点击登录按钮

  login_btn.click()

  再点击登录按钮后,12306服务器会弹出滑块验证的窗口,需要我们使用代码模拟浏览器点击滑动完成验证。这里我们使用动作链来完成一系列操作:点击并长按,向指定方向拖拽一定的距离。

  动作链使用

  这里我们在介绍下动作链的使用,使用动作链非常简单。

  1、导入相应的模块from selenium.webdriver import ActionChains

  2、用构造方法获取动作链对象

  # 定义动作链,点击并拖拽 

 aco = ActionChains(driver)

  # 点击并长按

  aco.click_and_hold(span)

  # 位移指定的距离

  aco.move_by_offset(25,0).perform()

  注意:想让动作链执行相应的操作必须调用.perform()方法。

  完成验证码滑块的拖动

  # 定义动作链,点击并拖拽

  aco = ActionChains(driver)

  # 点击并长按

  aco.click_and_hold(span)

  #perform()立即执行动作链操作

  for i in range(5):

      aco.move_by_offset(25,0).perform()

      time.sleep(0.3)

  # 释放动作链

  aco.release()

  规避检测

  由于12306会识别使用selenium模块的浏览器操作,为了规避相应的检测,我们还必须添加相应的代码。
 

 from selenium.webdriver import ChromeOptions

  chrome_options = Options()

  chrome_options.add_argument("--disable-blink-features=AutomationControlled")

  driver = webdriver.Chrome(executable_path='你的chromedriver路径',chrome_options=chrome_options)

  完整代码 

 from selenium import webdriver

  import requests

  from lxml import etree

  from selenium.webdriver import Chrome

  from selenium.webdriver import ChromeOptions

  from selenium.webdriver import ActionChains

  import time

  # 实现无可视化界面

  from selenium.webdriver.chrome.options import  Options

  chrome_options = Options()

  chrome_options.add_argument("--disable-blink-features=AutomationControlled")

  # 这里填入你自己的chromedriver的安装路径

  driver = webdriver.Chrome(executable_path='C:/Users/Declan/AppData/Local/Google/Chrome/Application/chromedriver',chrome_options=chrome_options)

  driver.get('https://kyfw.12306.cn/otn/resources/login.html')

  # 根据id获取用户账号输入框、密码输入框

  username_tag = driver.find_element_by_id('J-userName')

  password_tag = driver.find_element_by_id('J-password')

  # 填入自己的账号和密码

  username_tag.send_keys('xxxxxxxx')

  time.sleep(1)

  password_tag.send_keys('xxxxxx')

  # 根据id获取登录按钮

  login_btn = driver.find_element_by_id('J-login')

  # 点击登录按钮

  login_btn.click()

  # 这里必须得休眠,不然运行速度太夸,代码难以定位到滑块

  time.sleep(2)

  span = driver.find_element_by_css_selector('.btn_slide')

  # 定义动作链,点击并拖拽

  aco = ActionChains(driver)

  # 点击并长按

  aco.click_and_hold(span)

  #perform()立即执行动作链操作

  for i in range(5):

      aco.move_by_offset(25,0).perform()

      time.sleep(0.3)

  # 释放动作链

  aco.release()

  time.sleep(2)

  ok_btn = driver.find_element_by_css_selector('.ok')

  ok_btn.click()

  time.sleep(5)

  driver.quit()

猜你喜欢

转载自blog.csdn.net/qq_60168783/article/details/123583833