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 protected]",
"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()
New contributor
MrOmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.