当使用 ChromeDriver 和 Selenium 设置最大属性时,无法使用 send_keys 将日期作为文本发送到日期选择器字段
Unable to send date as text to datepicker field using send_keys when max attribute is set using ChromeDriver and Selenium
我正在尝试使用 chromedriver
下载一些文件。
我已经切换到 chromedriver
,因为在 firefox
中,我需要单击 link 打开一个新的 window,即使在所有必需的之后,下载对话框也会出现设置,我无法绕过它。
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>
字段,您需要为 element_to_be_clickable()
引入 WebDriverWait,您可以使用以下任一方法 :
使用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
我正在尝试使用 chromedriver
下载一些文件。
我已经切换到 chromedriver
,因为在 firefox
中,我需要单击 link 打开一个新的 window,即使在所有必需的之后,下载对话框也会出现设置,我无法绕过它。
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>
字段,您需要为 element_to_be_clickable()
引入 WebDriverWait,您可以使用以下任一方法
使用
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