【问题标题】:Selenium input date on disabled datepicker禁用日期选择器上的硒输入日期
【发布时间】:2018-11-21 01:11:40
【问题描述】:

我正在尝试输入我自己选择的日期,但我卡住了单击禁用的日期选择器。
如果我点击日期选择器,它会弹出并要求点击日期,即使一个月我也必须点击更多。
我很困惑该怎么做。

网址

http://finra-markets.morningstar.com/BondCenter/TRACEMarketAggregateStats.jsp

到目前为止我的代码。

from selenium import webdriver
from time import sleep
driver = webdriver.Chrome('path') # No Path Problem just changed here
driver.get('http://finra-markets.morningstar.com/BondCenter/TRACEMarketAggregateStats.jsp')
sleep(6)
date = driver.find_element_by_class_name("date-btn")
date.click()
sleep(4)
selector = driver.find_element_by_css_selector('[val="2017-10-11"]')
selector.click()
sleep(5)

这不起作用..!

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    首先,您需要单击“上个月”按钮,直到显示必要的年份和月份。 (在您的情况下,您需要 2017 年 10 月)。循环的代码可能如下所示:

    date = driver.find_element_by_class_name("date-btn")
    date.click()
    sleep(4)
    #getting element representing previous month button
    prev_month = driver.find_element_by_class_name("pm")
    #starting a loop that will click prev_month button untill calendar for October 2017 is shown
      # use .text because getText() does not work here
    while driver.find_element_by_class_name("titleCont").text != "Oct 2017"
      prev_month.click()
      #You can play around this sleep's value. or just remove it completely
      sleep(2)
    #Calendar should now be opened on Oct 2017 so we can look for desired date '11 october 2017
    selector = driver.find_element_by_css_selector('[val="2017-10-11"]')
    selector.click()
    sleep(5)
    

    当你让它工作时。考虑用智能等待替换睡眠 official doc, similar question answered

    【讨论】:

    • 谢谢。在收到 webelement 错误后,我进行了一些编辑以运行它。
    • 太棒了!感谢您的编辑。我的错......我不使用phyton所以有点猜测。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    相关资源
    最近更新 更多