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 ...
随机推荐
- 2019.10.22 csp-s模拟测试82 反思总结
重来重来,刚刚就当什么都没发生 今天的题属实有些迷惑,各种意义上…总之都很有难度吧.不满归不满,这套题的确不是什么没有意义的题目. 为了考验自己的学习能力记忆力,决定不写题解,扔个代码完事了 其实是懒 ...
- 关于Ajax提交后无法刷新iframe页面的问题
原因及解决方法: 要把刷新代码写进ajax的代码块里面,而不是放在后面
- 关于在eclipse中安装各种插件的问题
在eclipse中安装php插件的方法 参考转载链接:eclipse 安装php插件 并配置环境 elipse的php插件地址:https://www.eclipse.org/pdt/ 以下可能会用到 ...
- webpack学习之—— Code Spliting(代码分离)
代码分离特性能够把代码分离到不同的 bundle 中,然后可以按需加载或并行加载这些文件.代码分离可以用于获取更小的 bundle,以及控制资源加载优先级,如果使用合理,会极大影响加载时间. 有三种常 ...
- js读取解析JSON数据(转)
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- querySelector与getElementBy系列的区别
getElementBy系列 document.getElementsByTagName('tag'); document.getElementById('id'); document.getElem ...
- POJ3889Fractal Streets
Fractal Streets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 445 Accepted: 162 Des ...
- 关于JSON的个人理解
1.比xml更易于解析的数据存储方式 2.主要是用键值对的方式进行存储 3.可以用来存储对象或者是对象数组 个人感觉W3C上给的教程很好
- git安装方法
点击 http://git-scm.com/download 选择你使用的操作系统,如果是linux就可以看到命令行,windows则是下载客户端
- metro扁平UI网页组件
在线演示 本地下载