【问题标题】:How to enter datetime values in python selenium datepicker?如何在 python selenium datepicker 中输入日期时间值?
【发布时间】:2021-09-30 06:25:16
【问题描述】:

我有输入日期时间的页面,我有日期时间值作为字符串,我通过 find_element_by_xpath 传递它,我收到错误 这是我的 xpath

我在字符串变量中有值

value = '1980-06-30T00:00:00Z'

我用代码

xpath = '//*[@id="main-panel"]/div/app-manual-update/div/div/form/div[36]/div/input'
driver.find_element_by_xpath(xpath).send_keys(value);

我明白了

ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=94.0.4606.61)

HTML

如何传递日期时间值?

【问题讨论】:

    标签: python selenium datepicker


    【解决方案1】:

    你需要使用JS,如下:

    from datetime import datetime
    today_date = datetime.today().strftime('%Y-%m-%d')
    
    wait = WebDriverWait(driver, 30)
    date= wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id="main-panel"]/div/app-manual-update/div/div/form/div[36]/div/input")))
    driver.execute_script(f"arguments[0].setAttribute('value', '2021-06-30T00:00:00Z')", date)
    

    进口:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

    日期格式为yyyy-mm-dd 还要确保不要点击日期选择器,它会直接输入 输入字段中的

    【讨论】:

    • 谢谢,它运行没有错误,但值没有传递到输入框
    • 我需要查看 HTML,页面 url 是公开的吗?
    • 我想我明白了,看到 app-datetimepicker 的值是 attribute 你需要遵循那个模式。喜欢2021-06-30T00:00:00Z
    • 试试这个driver.execute_script("arguments[0].setAttribute('value', '2021-06-30T00:00:00Z')", date)
    • 是的,有效
    【解决方案2】:

    这里也可以使用arguments[0].value来设置属性值。

    HTML

    代码

    date = wd.find_element_by_id("dobundefined")
    driver.execute_script("arguments[0].value = '1980-06-30T00:00:00Z';", date) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多