pythonselenium-webdriverautomationbots

How can I click a dropdown menu and select a gender with Python


I am trying to create a Python program which can fill the form on this website: https://www.gewobag.de/fuer-mieter-und-mietinteressenten/mietangebote/0100-02559-0401-0074/

My first goal was to select "Anfrage senden" which means send a request and then the form appears. After that I wanted to select "Anrede" which means Salutation but it doesn't work.

I have written the following code:

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

# Replace with the path to your webdriver executable
geckodriver_path = '/usr/local/bin/geckodriver'

# Initialize the WebDriver for Firefox
driver = webdriver.Firefox(executable_path=geckodriver_path)

driver.maximize_window()  # For maximizing window
driver.implicitly_wait(20)  # Gives an implicit wait for 20 seconds

# Navigate to the form page
driver.get('https://www.gewobag.de/fuer-mieter-und-mietinteressenten/mietangebote/0100-02559-0401-0074/')

# Wait for the "Alle Cookies akzeptieren" button to be clickable and click it
cookies_accept_button = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, 'a._brlbs-btn-accept-all'))
)
cookies_accept_button.click()

# Wait for the "Anfrage Senden" button to be clickable and click it
try:
    anfrage_button = WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.XPATH, '//button[contains(text(), "Anfrage senden")]'))
    )
    driver.execute_script("arguments[0].scrollIntoView(true);", anfrage_button)  # Scroll to the button
    time.sleep(1)  # Give time for any scrolling animation to complete
    driver.execute_script("arguments[0].click();", anfrage_button)  # Click using JavaScript
except Exception as e:
    print("An error occurred while clicking the 'Anfrage Senden' button:", e)

# Use JavaScript to click the Anrede dropdown and select 'Herr'
try:
    anrede_dropdown = WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.XPATH, "//div[@formcontrolname='salutation']//div[contains(@class, 'ant-select-selection__placeholder')]"))
    )
    driver.execute_script("arguments[0].click();", anrede_dropdown)
    print("Anrede dropdown clicked")

    herr_option = WebDriverWait(driver, 30).until(
        EC.visibility_of_element_located((By.XPATH, "//ul[contains(@class, 'ant-select-dropdown-menu')]//li[@role='option' and contains(text(), 'Herr')]"))
    )
    driver.execute_script("arguments[0].scrollIntoView(true);", herr_option)  # Scroll to the option
    time.sleep(1)  # Give time for any scrolling animation to complete
    driver.execute_script("arguments[0].click();", herr_option)
    print("Herr option selected")
except Exception as e:
    print("An error occurred while selecting 'Herr':", e)



# List of user details to be filled in the form
user_details = [
    {
        "email": "email@gmail.com",
        "first_name": "samplename",
        "surname": "samplesurname",
        "street": "samplestreet",
        "house_number": "39",
        "postal_code": "23455",
        "city": "Samplecity"
    },
    {
        "email": "email2@exom",
        "first_name": "Jane",
        "surname": "Smith",
        "street": "456 Elm St",
        "house_number": "2B",
        "postal_code": "67890",
        "city": "Munich"
    },
   
]

wait = WebDriverWait(driver, 30)

driver.quit()

Solution

  • There is an IFRAME present in the DOM. In this case, you should first switch the driver into the IFRAME and then perform actions. Once all actions within the IFRAME are finished, then you should switch the driver back to default content.

    Check the working code with explanation below:

    import time
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Firefox()
    driver.maximize_window()
    wait = WebDriverWait(driver,10)
    
    driver.get("https://www.gewobag.de/fuer-mieter-und-mietinteressenten/mietangebote/0100-02559-0401-0074/")
    
    # Click "Alle Cookies akzeptieren"
    wait.until(EC.element_to_be_clickable((By.XPATH,"(//a[contains(text(),'Alle Cookies akzeptieren')])[1]"))).click()
    
    # Click `Anfrage senden` using JS
    anfrage_senden= wait.until(EC.element_to_be_clickable((By.XPATH, "(//button[contains(text(), 'Anfrage senden')])[1]")))
    driver.execute_script("arguments[0].click();", anfrage_senden)
    
    # Switch driver into IFRAME
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "contact-iframe")))
    
    # Click "Bitte auswählen']"
    wait.until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Bitte auswählen']"))).click()
    
    # Click dropdown option "Herr"
    wait.until(EC.element_to_be_clickable((By.XPATH, "//li[contains(text(),'Herr')]"))).click()
    
    # to come out of IFRAME
    driver.switch_to.default_content()
    time.sleep(20)