Selenium无法点击下拉菜单

0 投票
1 回答
37 浏览
提问于 2025-04-14 17:06

我正在尝试自动化抓取这个页面:https://www.immobilienscout24.de/

但是我在处理这个下拉菜单时遇到了麻烦:

这里插入图片描述

这部分的代码是:

apart_or_house = Select(driver.find_element(By.NAME, 'oss-rent'))
apart_or_house.select_by_index(2)

但是我一直收到这个错误信息:

追踪(最近的调用最后): 文件 "c:\Users\Zemmente\Documents\Python\google maps scraper\test_scraping.py",第41行,在 apart_or_house.select_by_index(2) 文件 "C:\Users\Zemmente\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\select.py", 第98行,在 select_by_index self._set_selected(opt) 文件 "C:\Users\Zemmente\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\select.py", 第213行,在 _set_selected 引发 NotImplementedError("您不能选择一个禁用的选项") NotImplementedError: 您不能选择一个禁用的选项

可能是什么问题呢,因为左边的下拉菜单是可以正常工作的?

附注:如果需要,这里是完整的代码:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

URL = "https://www.immobilienscout24.de/"

driver = webdriver.Chrome()
driver.get(URL)

# Wait for the page to load
time.sleep(4)

# Accepting cookies (if they appear)
try:
    def get_shadow_root(element):
        return driver.execute_script('return arguments[0].shadowRoot', element)

    shadow_host = driver.find_element(By.ID, 'usercentrics-root')
    button = get_shadow_root(shadow_host).find_element(By.CSS_SELECTOR, '[data-testid=uc-accept-all-button]')
    button.click()
except:
    print("Accept cookies not found.")

# City name
city_inputs = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="oss-location"]')))
city_input = [element for element in city_inputs if element.is_displayed()][0]
city_input.click()
city_input.send_keys(input('Enter the city name you want to search for: '))
time.sleep(1)
city_input.send_keys(Keys.ENTER)

# Buying, renting or building
while True:
    buying_or_renting_inputs = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="oss-form"]/article/div/div[1]/div/div[3]/div/div/select')))
    buying_or_renting_input = [element for element in buying_or_renting_inputs if element.is_displayed()][0]
    options = int(input('Enter the number of one of the options: 0-Rent, 1-Buying, 2-Building: '))
    if options < 0 or options > 2:
        options = int(input('Enter the number of one of the options: 0-Rent, 1-Buying, 2-Building: '))
    else:
        select_buy_or_rent = Select(buying_or_renting_input).select_by_index(options)
        break

# Map options to corresponding names
options_mapping = {
    0: 'oss-rent',
    1: 'oss-buy',
    2: 'oss-build'
}

# Get the corresponding name based on user input
selected_option_name = options_mapping.get(options)

# Update Apartmanent or house variable dynamically
apart_or_house = driver.find_element(By.NAME, selected_option_name)
options_2 = Select(apart_or_house).select_by_index(0)

# Search for the results
search_button = driver.find_element(By.XPATH, '//*[@id="oss-form"]/article/div/div[3]/button')
search_button.click()

# Handle captcha
captcha = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_radar_tip')))
captcha.click()

# Wait for the results to load
time.sleep(5)

page_source = driver.page_source

driver.quit()

soup = BeautifulSoup(page_source, "html.parser")

1 个回答

1

当你在左边的下拉菜单中选择一个值时,右边的下拉菜单也会跟着改变。

所以,左边的每一个选项在右边都有一个不同的对应项。

Mieten(0)-->oss-rent , Kaufen(1)--->oss-buy , Bauen(2)--->oss-build

比如,你可以把你的代码改成:

a={
    0:'oss-rent',
    1:'oss-buy',
    2:'oss-build',
}
index=1#Kaufen
buying_or_renting_inputs = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="oss-form"]/article/div/div[1]/div/div[3]/div/div/select')))
buying_or_renting_input = [element for element in buying_or_renting_inputs if element.is_displayed()][0]
select_buy_or_rent = Select(buying_or_renting_input)
select_buy_or_rent.select_by_index(index)
time.sleep(4)

apart_or_house_inputs = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.NAME, a[index])))
apart_or_house_input = [element for element in apart_or_house_inputs if element.is_displayed()][0]
select_apart_or_house = Select(apart_or_house_input)
options=select_apart_or_house.options
for i in range(len(options)):
    select_apart_or_house.select_by_index(i)
    time.sleep(4)

撰写回答