python selenium处理JS只读(12306)
2024-09-05 04:49:56
12306为例
js = "document.getElementById('train_date').removeAttribute('readonly');"
driver.execute_script(js)
time2获取当前时间 tomorrow_time 获取明天时间
from selenium import webdriver
import time
import datetime
time1=datetime.datetime.now().strftime("%Y-%m-%d.%H%M%S.%f")
time2=datetime.datetime.now().strftime("%Y-%m-%d")
oneday = datetime.timedelta(days=1)
tomorrow_time = datetime.datetime.now() + oneday
tomorrow_time = datetime.datetime.strftime(tomorrow_time, '%Y-%m-%d') driver = webdriver.Firefox()
# driver.maximize_window()
driver.implicitly_wait(6)
#页面不稳定
driver.get("http://www.12306.cn/mormhweb/")
driver.find_element_by_xpath("html/body/div[1]/div[4]/div[3]/div[1]/ul/li[3]/a").click()
time.sleep(1)
driver.switch_to.window(driver.window_handles[1])
driver.find_element_by_id("fromStationText").clear()
driver.find_element_by_id("fromStationText").send_keys('上海')
driver.find_element_by_xpath("//div[@id='form_cities']/div[@id='panel_cities']/div[@id='citem_0']").click()
time.sleep(2)
driver.find_element_by_id("toStationText").clear()
driver.find_element_by_id("toStationText").send_keys('九江')
driver.find_element_by_xpath("//div[@id='form_cities']/div[@id='panel_cities']/div[@id='citem_0']").click()
# 去除js read-only属性
js = "document.getElementById('train_date').removeAttribute('readonly');"
driver.execute_script(js)
# 用js方法输入日期
js_value = 'document.getElementById("train_date").value="'+time2+'"'
driver.execute_script(js_value)
time.sleep(1)
driver.find_element_by_id("a_search_ticket").click()
time.sleep(2)
try:
assert u'车票预订 | 客运服务 | 铁路客户服务中心'== driver.title
print("passed")
except Exception as e:
print("failed")
driver.quit()
最新文章
- How threads differ from processes
- Android中的Shape使用总结
- 快速诊断Linux性能
- Install the 64bit library in Ubuntu13.10
- js实现简单易用的上下无缝滚动效果
- jdbcTemplate 获取数据表结构
- WPF自定义窗口(Windows Server 2012 Style)
- 基于GBT28181:SIP协议组件开发-----------第三篇SIP注册流程分析实现
- WinCE 5.0模拟器,在 win7 下安装后, VS2008里不显示
- PHP安装phpredis扩展
- iOS 单利模式实现/优缺点
- libev学习代码
- css 的一些知识点的整理
- 一步一步 copy163: 网易严选 ---- vue-cli
- 如何安装mariadb服务器和解决 can't connect to local mysql server through socket...
- 根据传智写的SqlHelper
- 001.Linux开机启动过程
- 02:MongoDB操作
- Django之WSGI 和MVC/MTV
- 解决sublime的中文乱码
热门文章
- IO流8 --- 使用FileReader和FileWriter实现文本文件的复制 --- 技术搬运工(尚硅谷)
- 【风马一族_win10设置热点】win10无法开启热点怎么办
- 100个常用的原生JavaScript函数
- 微信小程序--轮播图,标题,盒子,tab栏的合成例子
- web标准中定义id与class有什么区别吗
- C# System.Timers.Timer中的坑,程序异常退出后timer依然运行问题
- WEB性能测试用例设计
- 2018-8-10-C#-快速释放内存的大数组
- JS实现AES加密并与PHP互通的方法分析
- Liferay Model Hints