我正在尝试创建一个可以填写此网站上的表格的Python程序: https://www.gewobag.de/fuer-mieter-und-mietinteressenten/mietangebote/0100-02559-0401-0074/
我的第一个目标是选择“Anfrage senden”,这意味着发送请求,然后表格就会出现。之后我想选择“Anrede”,这意味着称呼,但它不起作用。
我写了以下代码:
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()
检查工作代码并附有以下说明:
import time
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
driver = webdriver.Chrome()
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"
wait.until(EC.element_to_be_clickable((By.XPATH, "(//button[contains(text(), 'Anfrage senden')])[1]"))).click()
# 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)