51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 831|回复: 0
打印 上一主题 下一主题

[转贴] Python使用selenium模块模拟登录12306

[复制链接]
  • TA的每日心情
    无聊
    4 天前
  • 签到天数: 941 天

    连续签到: 3 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2022-4-28 10:10:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1测试积点
    selenium模块概述
      selenium模块是基于浏览器自动化的一个模块。换句话说使用selenium可以让浏览器根据自己写的代码自动运行。
      相应的语法
      ·编写基于浏览器自动化的操作代码
      · 发起请求:get(url)
      · 标签定位:find系列的方法
      · 标签交互:send_ keys( 'xxx')
      · 执行js程序:excute _script( 'jsCode ')一前进,后退:back() ,forward()
      · 关闭浏览器:quit()
      这里就不赘述selenium模块的安装了。
      登录的具体流程
      总体思路:
      · 使用selenium向12306网站发起请求,打开登陆页面
      · 根据标签的id值定位到用户账号框、用户密码框以及登录按钮
      · 填入账号密码后,点击登录按钮
      · 进行滑块验证
      · 登陆成功

    1.  # 根据id获取用户账号输入框、密码输入框
    2.   username_tag = driver.find_element_by_id('J-userName')
    3.   password_tag = driver.find_element_by_id('J-password')
    4.   # 填入自己的账号和密码
    5.   username_tag.send_keys('xxxxxxx')
    6.   time.sleep(1)
    7.   password_tag.send_keys('xxxxxxxx')
    8.   # 根据id获取登录按钮
    9.   login_btn = driver.find_element_by_id('J-login')
    10.   # 点击登录按钮
    11.   login_btn.click()
    复制代码
    再点击登录按钮后,12306服务器会弹出滑块验证的窗口,需要我们使用代码模拟浏览器点击滑动完成验证。这里我们使用动作链来完成一系列操作:点击并长按,向指定方向拖拽一定的距离。

    动作链使用
      这里我们在介绍下动作链的使用,使用动作链非常简单。
      1、导入相应的模块from selenium.webdriver import ActionChains
      2、用构造方法获取动作链对象
    1.  # 定义动作链,点击并拖拽
    2.   aco = ActionChains(driver)
    3.   # 点击并长按
    4.   aco.click_and_hold(span)
    5.   # 位移指定的距离
    6.   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模块的浏览器操作,为了规避相应的检测,我们还必须添加相应的代码。
    1. from selenium.webdriver import ChromeOptions
    2.   chrome_options = Options()
    3.   chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    4.   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()






    附件: 您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-4 07:20 , Processed in 0.062606 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表