首页 > 其他分享 >使用selenium自动化模块实现登录12306

使用selenium自动化模块实现登录12306

时间:2022-08-31 22:15:08浏览次数:87  
标签:selenium driver find 12306 sleep 模块 date element click

import time
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
chrome_options = Options()
# chrome_options.add_argument("--headless")
chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36')
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
js = 'window.navigator.webdriver '
js1 = 'return window.navigator.webdriver'
driver = Chrome(options=chrome_options)
driver.execute_script(js)
driver.get('https://www.12306.cn/index/')
user_login = driver.find_element(By.ID, 'J-btn-login')
user_login.click()
user_input = driver.find_element(By.ID, 'J-userName')
user_input.send_keys('User-ID')
time.sleep(2)
pwd_input = driver.find_element(By.ID, 'J-password')
pwd_input.send_keys('Password')
time.sleep(2)
login_button = driver.find_element(By.ID, 'J-login')
login_button.click()
time.sleep(2)
# Verifying Code Test
scroll_button = driver.find_element(By.ID, 'nc_1_n1z')
Action = ActionChains(driver=driver)
Action.click_and_hold(scroll_button)
for i in range(5):
Action.move_by_offset(80, 0).perform()
time.sleep(0.1)
Action.release()
result = driver.execute_script(js1)
print(result)
time.sleep(10)
# Enough Time To Load The Confirm Button
# Input Data And Chose Station
confirm_button = driver.find_element(By.XPATH, '//*[@role="alertdialog"]/div[3]/a')
confirm_button.click()
first_page_button = driver.find_element(By.XPATH, '//*[@id="J-index"]/a')
first_page_button.click()
station_input_from = driver.find_element(By.XPATH, '//*[@id="fromStationText"]')
station_input_from.click()
time.sleep(1)
station_input_from.send_keys('station from')
station_choose_from = driver.find_element(By.XPATH, '//*[@id="citem_0"]')
station_choose_from.click()
station_input_to = driver.find_element(By.XPATH, '//*[@id="toStationText"]')
station_input_to.click()
time.sleep(1)
station_input_to.send_keys('station to')
station_choose_to = driver.find_element(By.XPATH, '//*[@id="citem_0"]')
station_choose_to.click()

date_year_month = input('年月:')
date_day = input('日期:')
date_content = date_year_month+'-'+'0'+date_day
date_choose = driver.find_element(By.XPATH, '//*[@id="train_date"]')
date_choose.clear()
time.sleep(1)
date_choose.click()
time.sleep(1)
date_choose.send_keys(date_content)
refresh_click = driver.find_element(By.XPATH, '//*[@id="train_date"]')
refresh_click.click()
time.sleep(2)
date_xpath = '//*[@id="toolbar_Div"]/div[10]/div[1]/div[2]/'+'div['+date_day+']'
date_confirm_click = driver.find_element(By.XPATH, date_xpath)
date_confirm_click.click()
time.sleep(1)
search_button = driver.find_element(By.XPATH, '//*[@id="search_one"]')
search_button.click()
time.sleep(10)
# Get The Train Station Information
train_number = driver.find_element(By.XPATH, '//*[@id="t-list"]/table/tbody/tr')
print(train_number)
result = driver.execute_script(js1)
print(result)
time.sleep(3)

标签:selenium,driver,find,12306,sleep,模块,date,element,click
From: https://www.cnblogs.com/charleyhoot/p/16644673.html

相关文章

  • webpack css-loader模块化导致的antd样式不好用解决方案
    css-loader模块化会导致antd样式文件无效,解决的方式就是排除node_modules下的目标文件就可以如果你用的less就可以按以下方式来配置{test:/\.less$/,inclu......
  • 模块
    在编写程序的过程中把所有代码都写到一个脚本中肯定不便于维护和使用。python有一种方法可以把定义放在一个脚本里,并在其他脚本或解释器的交互式实例中使用他们。这样的......
  • 河北稳控科技使用标准信号检测 VM振弦采集模块测量精度
    河北稳控科技使用标准信号检测VM振弦采集模块测量精度(一) (1)电源1.1VDD引脚电源必须使用LDO稳压或者低纹波线性电源,LDO推荐使用AM1117_3.3V芯片,测试时发现SPX......
  • Selenium+Python微博爬虫实战
    读研后终日苦于写论文(zuoniuma),一年来也没时间更新微博,想想还是要在假期抽点时间,将一年所学记点笔记,或许日后还有些用处。惭愧的是这一年在快节奏的压迫下,很多知识没有......
  • gradle多模块项目打包依赖拆分
    一、目录结构.├──boogle-common│  ├──build.gradle.kts│  └──src├──boogle-core│  ├──build.gradle.kts│  └──src├──......
  • Python-常用内置模块
    常用内置模块数学计算模块math函数说明ceil(x)返回大于或等于x的最小整数floor(x)返回小于或等于x的最大整数sqrt(x)返回x的平方根pow(x,y)返......
  • 兼容树莓派引脚定义,适配多种模块--米尔百变STM32MP1开发板演示
    大家好,我是米尔的工程师,今天给大家带来一款全新的米尔板卡标杆产品:MYD-YA15XC-T。今天的文章将分成两部分来介绍这块基于STM32MP1处理器开发的板卡。一、百变开发板MYD-Y......
  • Jenkins+SpringCloud(多模块)+Vue项目详细配置
    一、Jenkins安装及所需插件安装   安装过程略。   我这用到工具包括JDK、Git、Maven、NodeJS:可以选择自行在服务器安装,也可以通过Jenkins自动安装,位置在系统......
  • selenium 常用操作汇总
    使用selenium1、查看Chrome版本去下载浏览器驱动驱动下载链接2、selenium官方网站官方文档selenium通信原理对于每一条Selenium脚本,一个http请求会被......
  • 13模块化编程
    1<!doctypehtml>2<htmllang="en">3<head>4<metacharset="UTF-8">5<metaname="viewport"6content="width=device-width,user-sca......