python - 当使用 ChromeDriver 和 Selenium 设置最大属性时,无法使用 send_keys 将日期作为文本发送到日期选择器字段

标签 python selenium google-chrome selenium-chromedriver webdriverwait

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

相关文章:

java - 如何使用 sikuli 和 selenium webdriver java 自动化 flash

python - 将输出重定向到 selenium 中的特定监视器

javascript - 由于传输层/元/连接调用,使用 faye/nodejs 在 firefox/chrome 上加载较长的初始页面

javascript - Google+ JavaScript API 登录弹出窗口不在 iOS 上返回 Chrome

python - 使用 pandas 计算时间序列中每个月的平均值

python - statsmodel 属性错误 : module 'scipy.stats' has no attribute 'chisqprob'

boolean 变量的 Python 枚举

python - 用于构建自动编码器的 keras.Flatten 的逆

powershell - 如何使用 Powershell 中的 Edge Selenium webdriver 检查窗口是否最小化

javascript - 为什么某些数组在 chrome 检查器中的值旁边显示有字母?