keys将日期作为文本发送到datepicker字段

keys将日期作为文本发送到datepicker字段

本文介绍了使用ChromeDriver和Selenium设置max属性时,无法使用send_keys将日期作为文本发送到datepicker字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用chromedriver下载一些文件.

I am trying to use chromedriver to download some files.

我已切换到chromedriver,因为在firefox中,我需要单击的链接会打开一个新窗口,并且即使在进行了所有必需的设置之后,我仍然无法解决下载对话框.

I have switched to chromedriver because in firefox the link I need to click opens a new window and the download dialog box appears even after all the required settings and I wasn't able to get around it.

chromedriver可以很好地用于下载,但是对于下面的元素,我似乎不太喜欢send_keys(),它可以在firefox上运行,但似乎无法使其正常工作.

chromedriver works fine for the download but I can't seem to send_keys() to the element below, it works on firefox but can't seem to get it to work on this.

<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>字段,理想情况下,您需要为element_to_be_clickable()引入 WebDriverWait ,并且可以使用以下任一定位器策略:

To send a character sequence to an <input> field ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • 使用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")
    

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

  • Note : You have to add the following imports :

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

  • 这篇关于使用ChromeDriver和Selenium设置max属性时,无法使用send_keys将日期作为文本发送到datepicker字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-31 01:28