The dropdown menu is unfortunately unresponsive to Selenium's attempts to click on it

Currently, I am working on automating the scraping process for a specific webpage:

The main challenge I am facing involves dealing with a dropdown menu:

https://i.stack.imgur.com/BCO7y.jpg

This is the code snippet related to that particular section:

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

Unfortunately, every time I execute this code, it throws an error message:

Traceback (most recent call last): File "c:\Users\Zemmente\Documents\Python\google maps scraper\test_scraping.py", line 41, in apart_or_house.select_by_index(2) File "C:\Users\Zemmente\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\select.py", line 98, in select_by_index self._set_selected(opt) File "C:\Users\Zemmente\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\select.py", line 213, in _set_selected raise NotImplementedError("You may not select a disabled option") NotImplementedError: You may not select a disabled option

Could anyone help me identify what could be causing this issue since the left dropdown menu functions correctly?

P.S. If needed, here is the complete code block:

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)

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

# Handling cookie consent (if required)
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 input processing
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)

# Selection between 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

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

# Dynamically updating the Apartmanent or house variable based on the selection
selected_option_name = options_mapping.get(options)
apart_or_house = driver.find_element(By.NAME, selected_option_name)
options_2 = Select(apart_or_house).select_by_index(0)

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

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

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

page_source = driver.page_source

driver.quit()

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

Answer №1

Changing the value in the left dropDown will trigger a corresponding change in the right dropDown.

Each option on the left corresponds to a different element name on the right.

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

For instance, you can modify your code as follows:

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)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using the pandas library, you can save and manage numerous data sets within a single h5 file by utilizing the pd

If I have two different dataframes, import pandas as pd df1 = pd.DataFrame({'col1':[0,2,3,2],'col2':[1,0,0,1]}) df2 = pd.DataFrame({'col12':[0,1,2,1],'col22':[1,1,1,1]}) After successfully storing df1 with the comm ...

Determining the count of series using Python's Pandas

I needed to determine the number of series contained within a specific dataset. The count of time-series information was required for analysis. https://i.stack.imgur.com/VHQvw.png Within this context, I wanted users to select how they wished to analyze ...

Experiencing difficulties launching InstaPy

After successfully setting up selenium and geckodriver to work with firefox, I decided to give InstaPy a try for the first time. With firefox installed, I was able to launch the browser, go to Instagram, and log in with the code snippet below: browser ...

Error 111 encountered in Python when using HtmlUnit and Selenium in combination

Attempting to integrate selenium with HtmlUnit into my Django application. This is the process I'm following: To start in the background, I use: java -jar selenium-server-standalone-2.27.0.jar bg Here is the code snippet: from selenium.webdriver.co ...

What is the method to handle a missing element in a Selenium test using Python and return a Null value?

In my Selenium script using Python, I am extracting data from a website. However, some pages I'm scraping may not have certain elements present, causing a NoSuchElementException error to be thrown. I want to handle this by returning a null value when ...

The Selenium driver's execute_script method yields no result

One of the tasks I have is to determine if a specific element is within view. If it is, I want the script to return True; otherwise, False: driver.execute_script('window.pageYOffset + document.querySelector(arguments[0]).getBoundingClientRect().bottom ...

Looking for a website to conduct load testing? You'll need to log in first in order to access

I am in the process of developing an Automated Testing Python Project to conduct comprehensive testing on a website. This project involves executing Functionality, Accessibility, and Load Testing on the website, with detailed reporting of the outcomes. The ...

Python's idiomatic way of handling zero checks

What is the best practice for testing if a value is equal to zero in Python? if n == 0: Alternatively, if not n: One may argue that the first approach is more explicit, but empty sequences or None are commonly checked using the second method. ...

Automated Menu Selection using Selenium

I'm currently facing a challenge in writing a Python script using Selenium to interact with a webpage. I am struggling to use the .click() method to select an expandable list on the page. Despite successfully logging in and navigating to the desired p ...

Navigating a Dropdown menu using Selenium: A step-by-step guide

Actions actions = new Actions(driver); WebElement mainMenu = driver.findElement(By.xpath(".//*[@id='yui-gen2']/a")); actions.moveToElement(mainMenu).build().perform(); WebElement subMenu = driver.findElement(By.xpath(".//*[@id='helpAbout&apo ...

What are the best practices for managing certificates with Selenium?

I am currently utilizing Selenium to initiate a browser. What is the best approach for handling webpages (URLs) that prompt the browser to accept or deny a certificate? In the case of Firefox, I sometimes encounter websites that require me to accept their ...

Is it possible to use Python regex to insert commas back into a string?

I am working with a string that contains various dates such as March 4 1998, however, all the commas are missing. I need to figure out how to use Python to insert the commas back into these strings. The dates in question are within a lengthy paragraph. ...

Incorporate user profile images into the UserPostListView feature of Django

I am currently in the process of building my blog using Django, but as a newbie to this tool, I am facing challenges when it comes to displaying the user's profile picture in their posts view. In the users' post view, I have set up a filter at w ...

Errors in SQL syntax are being triggered by MySQL prepared statements

When I attempt to use prepared statements for my SQL insert query, an error in the syntax is returned. I decided to test the query using PHPMyAdmin and substituted the placeholders with actual values. Surprisingly, the query worked perfectly, leading me t ...

Calculate the mean of a subset of rows within various pandas columns

Looking to smooth out geographical data using a dataset by finding the nearest neighbors within a specific radius for each row, then calculating the mean and adding it as a new column. The code snippet below achieves this: import pandas as pd import numpy ...

Enhance a dataset by incorporating information from a different source

I have compiled a variety of datasets with different information that I now wish to combine. Here is an example of two datasets: Customer1 Customer2 Relationship Age_of_Relationship Alfa Wolk 1 12 C ...

Checking the validity of an HTTP response using Python

I am currently in the process of creating a basic web server and I have written a Python function to handle requests. Here is what it looks like: def handle_request(client_connection): request = client_connection.recv(1024) print(request.decode()) ...

Tips for preserving leading zeros during the conversion process from XML to CSV

I have encountered an issue with my code. When I view the DateTime in the XML file, it is 01052022000000000, but when I try to access it using Python, it appears as 1052022000000000 (the left zero is missing). I attempted to resolve this by using the func ...

The Python input function allows users to input data into a

Understanding the split() function in Python can be confusing at times, especially when you see it implemented like this: def sayHello(): name = input("What's your name?:" ) print("Hello", name) When you only require one input from the user, ...

How can I create a reusable function in Java Selenium WebDriver that can be called multiple times?

@Test(priority = 0) public void platformAdminLogin() { // logging in as a platform admin using correct credentials try { driver.findElement(By.id("emailId")).sendKeys("example@example.com"); driver.findElement(By.id("password")).sen ...