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