Home > Enterprise >  How to select calender month using Selenium Select method using python selenium
How to select calender month using Selenium Select method using python selenium

Time:07-19

I am trying to select a month from the calender using Select method of selenium in python. URL of site is: https://www.vegasinsider.com/mlb/matchups/ . I am getting timeout error and the driver is unable to find the select element of calender. Code is as follows:

wait = WebDriverWait(driver, 10)
click_calender=wait.until(
            EC.presence_of_element_located((By.ID, 'datepicker'))
        )
click_calender.click()
select_month = Select(wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="pika-title-yj"]/div[1]/select'))))
select_month.select_by_visible_text("June")

I am getting a timeout error on Select method as this:

select_month = Select(wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="pika-title-yj"]/div[1]/select'))))

File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Stacktrace: Backtrace:
        Ordinal0 [0x00636463 2188387]
        Ordinal0 [0x005CE461 1762401]
        Ordinal0 [0x004E3D78 802168]
        Ordinal0 [0x00511880 989312]
        Ordinal0 [0x00511B1B 989979]
        Ordinal0 [0x0053E912 1173778]
        Ordinal0 [0x0052C824 1099812]
        Ordinal0 [0x0053CC22 1166370]
        Ordinal0 [0x0052C5F6 1099254]
        Ordinal0 [0x00506BE0 945120]
        Ordinal0 [0x00507AD6 948950]
        GetHandleVerifier [0x008D71F2 2712546]
        GetHandleVerifier [0x008C886D 2652765]
        GetHandleVerifier [0x006C002A 520730]
        GetHandleVerifier [0x006BEE06 516086]
        Ordinal0 [0x005D468B 1787531]
        Ordinal0 [0x005D8E88 1805960]
        Ordinal0 [0x005D8F75 1806197]
        Ordinal0 [0x005E1DF1 1842673]
        BaseThreadInitThunk [0x774CFA29 25]
        RtlGetAppContainerNamedObjectPath [0x779E7A9E 286]
        RtlGetAppContainerNamedObjectPath [0x779E7A6E 238]

I have tried to click the month element but I am unable to do so as well. Please let me know what is wrong here.

CodePudding user response:

The dates of the calendar are <button> tags

vegasinsider

So you can't use Select() class.


Solution

To chose a date e.g. July 3rd, 2022 instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following css_selector based locator strategies:

  • Code block:

    driver.get('https://www.vegasinsider.com/mlb/matchups/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#datepicker"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-pika-year='2022'][data-pika-month='6'][data-pika-day='3']"))).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
    
  • Related