一尘不染

使用ChromeDriver和Selenium设置max属性时,无法使用send_keys将日期作为文本发送到datepicker字段

selenium

我试图用来chromedriver下载一些文件。

我已切换到该位置,chromedriver是因为在firefox该链接中,我需要单击以打开一个新窗口,并且即使在完成所有必需的设置后仍会出现下载对话框,而我却无法解决它。

chromedriver可以很好地进行下载,但是send_keys()对于下面的元素我似乎不太了解,它可以在Firefox上运行,但似乎无法在此上使用它。

<input name="" value="" id="was-returns-reconciliation-report-start-date" type="date" class="was-form-control was-input-date" data-defaultdate="" data-mindate="" data-maxdate="today" data-placeholder="Start Date" max="2020-02-12">

我努力了:

el = driver.find_element_by_id("was-returns-reconciliation-report-start-date")
el.clear()
el.send_keys("2020-02-01")
el.send_keys(Keys.ENTER)  # Separately

# Tried without clear as well
# no error but the date didn't change in the browser

driver.execute_script("document.getElementById('was-returns-reconciliation-report-start-date').value = '2020-01-05'")

# No error and no change in the page

阅读 471

收藏
2020-06-26

共1个答案

一尘不染

<input>理想情况下,要将字符序列发送到字段,您需要为引入 WebDriverWait
element_to_be_clickable()并且可以使用以下定位策略之一:

  • 使用ID

    el = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.ID, "was-returns-reconciliation-report-start-date")))
    

    el.clear()
    el.send_keys(“2020-02-12”)

  • 使用CSS_SELECTOR

    el = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "input.was-form-control.was-input-date#was-returns-reconciliation-report-start-date")))
    

    el.clear()
    el.send_keys(“2020-02-12”)

  • 使用XPATH

    el = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//input[@class='was-form-control was-input-date' and @id='was-returns-reconciliation-report-start-date']")))
    

    el.clear()
    el.send_keys(“2020-02-12”)

  • 注意 :您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    

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

2020-06-26