Python Selenium 在 ng datepicker 中输入日期

问题描述 投票:0回答:2

我正在尝试将日期输入到日期选择器的文本部分,但 send_keys 不起作用。

这是 html:

<date-picker _ngcontent-bdp-c11="" _nghost-bdp-c3="">
    <div _ngcontent-bdp-c3="" class="datePicker formDiv-header datePickerContainer">
        <div _ngcontent-bdp-c3="" class="formLabel-header">
            <!---->
            <label _ngcontent-bdp-c3="" for="dateInput1">Search Date:</label>
            <!---->
        </div>
        <div _ngcontent-bdp-c3="" class="formField-header" style="padding:0;">
            <div _ngcontent-bdp-c3="" class="input-group date" id="dateTimePicker1">
                <input _ngcontent-bdp-c3="" class="form-control formField-header" maxlength="10" type="text">
                <span _ngcontent-bdp-c3="" class="input-group-addon"><span _ngcontent-bdp-c3="" class="glyphicon glyphicon-calendar">
                </span>
            </span>
        </div>
    </div>
</div>
</date-picker>
<search-button _ngcontent-bdp-c11="" _nghost-bdp-c4="">
    <button _ngcontent-bdp-c4="" class="selectionButton" type="button"> Search
</button></search-button>

这是我的代码:

dateInputfield = driver.find_element_by_xpath('// *[ @ id = "dateTimePicker1"] / input')
# the dateInputfield:  <selenium.webdriver.remote.webelement.WebElement (session="6911b2bc422dac02d926b3b36429e8de", element="0.8362094047920967-3")>
dateInputfield.send_keys('01/15/2021')
searchbt = driver.find_element_by_xpath('//*[@id="filters"]/search-button/button')
searchbt.click()

或者,我尝试单击日历图标,这实际上会打开日历进行选择,但我不知道如何导航到给定日期以选择它。

calicon = driver.find_element_by_xpath('//*[@id="dateTimePicker1"]/span/span')
calicon.click()

如有任何帮助,我们将不胜感激。

python selenium-webdriver xpath datepicker css-selectors
2个回答
3
投票

要将日期字符发送到元素,您可以使用以下任一定位器策略

  • 使用

    css_selector

    driver.find_element(By.CSS_SELECTOR, "div.input-group.date#dateTimePicker1 > input.form-control.formField-header").send_keys('01/15/2021')
    
  • 使用

    xpath

    driver.find_element(By.XPATH, "//div[@class='input-group date' and @id='dateTimePicker1']/input[@class='form-control formField-header']").send_keys('01/15/2021')
    

理想情况下,要将 date characters 发送到您需要为 element_to_be_clickable() 引发

WebDriverWait
的元素,并且您可以使用以下任一定位器策略

  • 使用

    CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.input-group.date#dateTimePicker1 > input.form-control.formField-header"))).send_keys('01/15/2021')
    
  • 使用

    XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='input-group date' and @id='dateTimePicker1']/input[@class='form-control formField-header']"))).send_keys('01/15/2021')
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

0
投票

是的,在发送日期之前不要忘记使用 .clear() 方法清空元素内容!

请参考此代码:

day_picker=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, "YOUR XPATH...")))
day_picker.clear()
day_picker.send_keys("YOUR DATE")
day_picker.send_keys(Keys.ENTER) #optionnel..
wait = WebDriverWait(driver, 50)
© www.soinside.com 2019 - 2024. All rights reserved.