问题
I am trying to use chromedriver to download some files.
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 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">
I have tried:
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
回答1:
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:
Using
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")Using
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")Using
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
来源:https://stackoverflow.com/questions/60194403/unable-to-send-date-as-text-to-datepicker-field-using-send-keys-when-max-attribu