【问题标题】:How to click on a date within the calendar control in the website using Selenium如何使用 Selenium 在网站的日历控件中单击日期
【发布时间】:2022-08-14 00:49:46
【问题描述】:

我正在尝试使用 selenium 从本网站的日历中选择日期:

https://www.cbe.org.eg/en/Auctions/Pages/AuctionsEGPTBillsHistorical.aspx

我想从1st of december 2021 开始,但我一直得到的日期是1st of august 2022。有什么帮助吗?

这是我使用的代码:

pip install selenium
from selenium import webdriver
from selenium.webdriver.support.select import Select
path=\"C:\\Program Files\\chromedriver.exe\"
driver =webdriver.Chrome(path)
driver.get(\"https://www.cbe.org.eg/en/Auctions/Pages/AuctionsEGPTBillsHistorical.aspx\")
from_date=driver.find_element(\"id\",\"ctl00_ctl54_g_1eef16cc_149b_4250_b1db_366c6f7aa7e6_imgFromPopup\")
beg_day=driver.find_element(\"id\",\"ctl00_ctl54_g_1eef16cc_149b_4250_b1db_366c6f7aa7e6_CalendarFromDate_day_0_1\")
from_date.click()
beg_day.click()

    标签: python selenium xpath css-selectors calendar


    【解决方案1】:

    如果您的目标是推进表单的结果(而不是测试日历等的功能),那么您可以执行以下操作:

    from selenium.common.exceptions import NoSuchElementException, TimeoutException
    from selenium import webdriver
    from selenium.webdriver.firefox.service import Service
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.firefox.options import Options as Firefox_Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.support import expected_conditions as EC
    import time as t
    
    firefox_options = Firefox_Options()
    
    firefox_options.add_argument("--width=1280")
    firefox_options.add_argument("--height=720")
    # firefox_options.headless = True
    firefox_options.set_preference("general.useragent.override", "Mozilla/5.0 (Linux; Android 7.0; SM-A310F Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36 OPR/42.7.2246.114996")
    
    driverService = Service('chromedriver/geckodriver')
    
    browser = webdriver.Firefox(service=driverService, options=firefox_options)
    
    url = 'https://www.cbe.org.eg/en/Auctions/Pages/AuctionsEGPTBillsHistorical.aspx'
    
    browser.get(url) 
    
    start_date = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.XPATH,'//div[@class="date-picker"]')))[0]
    start_date_field = start_date.find_element(By.TAG_NAME, 'input')
    
    end_date = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.XPATH,'//div[@class="date-picker"]')))[1]
    end_date_field = end_date.find_element(By.TAG_NAME, 'input')
    browser.execute_script('arguments[0].value = "01/08/2022";', start_date_field)
    browser.execute_script('arguments[0].value = "08/08/2022";', end_date_field)
    

    这将在这些字段中输入日期,您可以进一步单击“在线查看”并查看结果。 linux上的设置是firefox/geckodriver,但是你只需要导入相关的包,定义好浏览器/驱动后观察部分。硒文档:https://www.selenium.dev/documentation/

    【讨论】:

    • 不客气,@M_william 如果回复解决了您的问题,请不要忘记将其标记为已接受(投票按钮下方的绿色标记)
    【解决方案2】:

    这个元素...

    driver.find_element("id","ctl00_ctl54_g_1eef16cc_149b_4250_b1db_366c6f7aa7e6_CalendarFromDate_day_0_1")
    

    表示日期Monday, August 01, 2022


    解决方案

    点击日期Monday, August 01, 2022您需要为element_to_be_clickable() 诱导WebDriverWait,您可以使用以下任一locator strategies:

    • 使用CSS_SELECTOR:

      driver.execute("get", {'url': 'https://www.cbe.org.eg/en/Auctions/Pages/AuctionsEGPTBillsHistorical.aspx'})
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id$='imgFromPopup']"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "table[id$='CalendarFromDate_daysTable'] tbody td > div[id$='CalendarFromDate_day_0_1']"))).click()
      
    • 使用路径:

      driver.execute("get", {'url': 'https://www.cbe.org.eg/en/Auctions/Pages/AuctionsEGPTBillsHistorical.aspx'})
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@id, 'imgFromPopup')]"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table[contains(@id, 'CalendarFromDate_daysTable')]//tbody//td/div[contains(@id, 'CalendarFromDate_day_0_1')]"))).click()
      
    • 笔记:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
    • 浏览器快照:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多