码迷,mamicode.com
首页 > Web开发 > 详细

练习js——自动化实现12306火车票查询

时间:2020-01-22 18:11:57      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:字符   email   com   rom   inpu   doc   mst   from   one   

# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2019/12/1 09:03
# @Author : "小多肉"
# @Email : 1021181701@qq.com
# @File : task1127.py
# @Software: PyCharm
import time
from datetime import datetime, timedelta

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.12306.cn/index/")
# 等待出发地选择框出现
# WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,"fromStationText")))
time.sleep(2)
#选择出发地
fromStation = 福州   #出发地汉字
fs = FZS   # 出发地代码
fromStation_js = """
document.getElementById(‘fromStation‘).value = arguments[0];  // 隐藏的出发地元素,value写入出发地代码
var from = document.getElementById(‘fromStationText‘);
from.className = "input inp-txt_select";    // 输入出发地后的class改成input inp-txt_select
from.value = arguments[1];
"""
driver.execute_script(fromStation_js,fs,fromStation)

#选择目的地
toStation = 长沙    # 出发地汉字
ts = CSQ  # 出发地代码
ele_toStation = driver.find_element_by_id("toStationText")
ele_ts = driver.find_element_by_id("toStation")
toStation_js = """arguments[1].value=arguments[2];
var to = arguments[0];
to.className = "input inp-txt_select";
to.value = arguments[3];
"""
driver.execute_script(toStation_js,ele_toStation,ele_ts,ts,toStation)

# 选择出发日期
# 当前日期+10天,转成字符串格式
date = str(datetime.now().date()+timedelta(days=10))    
# 使用js修改日期框为可输入状态,传入日期
date_js = """
var date = document.getElementById(‘train_date‘);
date.readOnly = false;
date.value = arguments[0];
"""
driver.execute_script(date_js,date)
# WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,"search_one")))
# 点击查询按钮
driver.find_element_by_id("search_one").click()
time.sleep(1)
driver.quit()

练习js——自动化实现12306火车票查询

标签:字符   email   com   rom   inpu   doc   mst   from   one   

原文地址:https://www.cnblogs.com/momoon/p/12228899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!