• 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之面向对象新式类和经典类
    Python之面向对象继承和派生
    Python之面向对象类和对象
    Python之面向对象的程序设计
    Python之面向对象函数式编程
    Python之内置函数
    列表解析与生成器表达式
    03: 交换机基本原理与配置
    02: 网络布线与数制转换
    01:数据封装解封的过程
  • 原文地址:https://www.cnblogs.com/yye2010/p/8651672.html
Copyright © 2020-2023  润新知