当使用 ChromeDriver 和 Selenium 设置最大属性时,无法使用send_keys将日期作为文本发送到日



我正在尝试使用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

要将字符序列发送到<input>字段,理想情况下,您需要诱导WebDriverWaitelement_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
    

最新更新