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()
原文地址:https://www.cnblogs.com/yye2010/p/8651672.html