我正在尝试使用 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>
理想情况下,您需要为 element_to_be_clickable()
引入 WebDriverWait您可以使用以下任一项 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")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
关于python - 当使用 ChromeDriver 和 Selenium 设置最大属性时,无法使用 send_keys 将日期作为文本发送到日期选择器字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60194403/