Selenium 无法点击下拉菜单

问题描述 投票:0回答:1

我正在尝试自动化此页面的抓取过程: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 地图 scraper est_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 中 raise 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
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

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

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

time.sleep(4)

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_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_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(2)

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

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

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

time.sleep(5)

page_source = driver.page_source

driver.quit()

soup = BeautifulSoup(page_source, "html.parser")
python selenium-webdriver
1个回答
0
投票

当您更改左侧下拉列表中的值时,右侧下拉列表也会更改。

因此,对于左侧的每个选项,右侧都有一个具有不同名称的元素

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

例如将您的代码更改为:

select_buy_or_rent.select_by_index(2)#Bauen

apart_or_house = Select(driver.find_element(By.NAME, 'oss-build'))
apart_or_house.select_by_index(0)
© www.soinside.com 2019 - 2024. All rights reserved.