python selenium处理JS只读(12306)
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()
python selenium处理JS只读(12306)的更多相关文章
- Python selenium的js扩展实现
python写的数据采集,对一般有规律的页面用 urllib2 + BeautifulSoup + 正则就可以搞定. 但是有些页面的内容是通过js生成,或者通过js跳转的,甚至js中还加入几道混淆机制 ...
- python selenium --调用js
转自:http://www.cnblogs.com/fnng/p/3230768.html 本节重点: 调用js方法 execute_script(script, *args) 在当前窗口/框架 同步 ...
- 【Python + Selenium】之JS定位总结
感谢:小琰子 Python+Selenium 脚本中的一些js的用法汇总: 1.滚动条 driver.set_window_size(500,500) js = "window.scroll ...
- 使用Python + Selenium破解滑块验证码
在前面一篇博客<使用 Python + Selenium 打造浏览器爬虫>中,我介绍了 Selenium 的基本用法和爬虫开发过程中经常使用的一些小技巧,利用这些写出一个浏览器爬虫已经完全 ...
- 如何用python抓取js生成的数据 - SegmentFault
如何用python抓取js生成的数据 - SegmentFault 如何用python抓取js生成的数据 1赞 踩 收藏 想写一个爬虫,但是需要抓去的的数据是js生成的,在源代码里看不到,要怎么才能抓 ...
- Python+Selenium WebDriver API:浏览器及元素的常用函数及变量整理总结
由于网页自动化要操作浏览器以及浏览器页面元素,这里笔者就将浏览器及页面元素常用的函数及变量整理总结一下,以供读者在编写网页自动化测试时查阅. from selenium import webdrive ...
- python selenium+phantomjs alert()弹窗报错
问题:用selenium+phantomjs 模拟登陆,网页用JavaScript的alert("登陆成功")弹出框,但是用switch_to_alert().accept()报错 ...
- WEB自动化(Python+selenium)的API
在做Web自动化过程中,汇总了Python+selenium的API相关方法,给公司里的同事做了第二次培训,分享给大家 ...
- 利用 Python + Selenium 实现对页面的指定元素截图(可截长图元素)
对WebElement截图 WebDriver.Chrome自带的方法只能对当前窗口截屏,且不能指定特定元素.若是需要截取特定元素或是窗口超过了一屏,就只能另辟蹊径了. WebDriver.Phant ...
随机推荐
- RHEL7系统安装方式
RHEL7系统安装方式包括: 1. 手动安装(介质在本地): 此种方式你可以通过图形界面操作定制你所需安装系统的配置及所需软件包等 优点:直观 缺点:效率低下,配置的东西多时易犯错 此种方式仅适用于初 ...
- 操作系统 Lab1
练习1 1 ucore.img 是如何生成的 使用 make V= 查看详细的步骤 cc kern/init/init.c 使用cc工具进行预处理 gcc -Idir 将dir 作为查找目录(头文件) ...
- NOIP模拟17.9.21
NOIP模拟17.9.21 3 58 145 201 161.5 样例输出21.6 数据规模及约定对于40% 的数据,N <= 20对于60% 的数据,N <= 1000对于100% 的数 ...
- 模拟21 题解(waiting)
留坑待填 效率!!! 题还没改Oh,NO!!!
- Vue--findIndex方法的使用原理
原理: // 根据id得到下标 // 默认去遍历list集合,将集合中的每个元素传入到function的item里, var index = this.list.findIndex(function( ...
- 一位AI研究员+区块链创业者的终极展望:AI DAO将统治世界
一位AI研究员+区块链创业者的终极展望:AI DAO将统治世界 [日期:2017-01-09] 来源:infoq.com 作者:杨赛 [字体:大 中 小] Trent McConaghy是一位资深的 ...
- Python使用Pandas高效处理测试数据
转自:https://www.cnblogs.com/keyou1/p/10948796.html 一.思考 1.Pandas是什么? 功能极其强大的数据分析库 可以高效地操作各种数据集 csv格式的 ...
- 地不安装Oracle,plsql远程连接数据库
由于Oracle的庞大,有时候我们需要在只安装Oracle客户端如plsql.toad等的情况下去连接远程数据库,可是没有安装Oracle就没有一切的配置文件去支持.最后终于发现一个很有效的方法,Or ...
- ConcurrentModificationException解决办法
package test.my.chap0302; import java.util.ArrayList; import java.util.Iterator; import java.util.Li ...
- git pull 提示错误,Your local changes to the following files would be overwritten by merge
error: Your local changes to the following files would be overwritten by merge: Please commit your c ...