[Python] python selenium find_element method [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    i'm testing a selenium script to click "Weiter zur Terminauswahl" button, check if there is any available appointment, and refresh if not for the following webpage:

    https://stadt.muenchen.de/buergerse...ng.html#/services/10339027/locations/10187259

    My script looks like that:

    #!/usr/bin/env python3


    import time
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException, TimeoutException
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    # Configuration
    URL = "https://stadt.muenchen.de/buergerservice/terminvereinbarung.html#/services/10339027/locations/10187259"
    REFRESH_INTERVAL = 10 # Seconds between refreshes.
    TARGET_DATE = "2025-04-10" # Desired date (format: YYYY-MM-DD).

    # Selector definitions:
    # Selector for the "next step" button that expands the calendar.
    NEXT_STEP_BUTTON_SELECTOR = '//button[contains(text(), "Weiter zur Terminauswahl")]'
    # Template for a calendar cell with a specific target date.
    # This example assumes each <td> in the calendar has a data-date attribute.
    CALENDAR_DATE_SELECTOR_TEMPLATE = '//td[@data-date="{date}"]'
    NEXT_STEP_BUTTON_XPATH='//*[@id="panel1"]/div/div/div/div[2]/button'


    def main():
    # Initialize the WebDriver (Chrome in this case).
    driver = webdriver.Chrome()

    try:
    driver.get(URL)
    print("Navigated to URL. Initiating appointment selection process.")

    while True:
    driver.refresh()
    print("Page refreshed. Looking for 'Weiter zur Terminauswahl' button to expand calendar...")

    try:
    next_step_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CLASS_NAME, 'm-button m-button--primary m-button--animated-right button-next'))
    )
    next_step_button.click()
    print("'Weiter zur Terminauswahl' button clicked. Calendar should now be visible.")
    except TimeoutException:
    print("Timeout: 'Weiter zur Terminauswahl' button was not available/clickable. Reattempting refresh cycle...")
    continue # Proceed to the next loop iteration.

    # Construct the XPath for the desired calendar date...


    As you can see, i checked the html-> inspect elements and tried different methods(XPATH, text, class name). But it still cannot find that button at all and will jump to TimeoutException. i'm not familiar with html. What is the correct method to make this script work?

    Continue reading...

Compartilhe esta Página