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

# !/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()
上一篇:python实战之通过爬虫实现火车票查询


下一篇:JS函数设置默认参数值