I'm trying to select date from calendar in this website using selenium:
https://www.cbe.org.eg/en/Auctions/Pages/AuctionsEGPTBillsHistorical.aspx
I want to begin from 1st of december 2021
but the date I keep getting is 1st of august 2022
. Any help?
This is the code I use:
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()
CodePudding user response:
If your goal is to advance to the results of the form (and not testing the functionality of calendar etc), then you can do the following:
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[@]')))[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[@]')))[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)
This will input the dates in those fields, and you can further click on 'View online' and see the results. Setup is firefox/geckodriver on linux, but you just have to import the relevant packages, and observe the part after defining the browser/driver. Selenium documentation: https://www.selenium.dev/documentation/
CodePudding user response:
This element...
driver.find_element("id","ctl00_ctl54_g_1eef16cc_149b_4250_b1db_366c6f7aa7e6_CalendarFromDate_day_0_1")
indicates the date Monday, August 01, 2022
Solution
To click on the date Monday, August 01, 2022
you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using 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()
Using XPATH:
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()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot: