Home > OS >  How to select element(s) from dropdown menu using Selenium?
How to select element(s) from dropdown menu using Selenium?

Time:01-14

I'm trying to send parameters to the input fields and then click search using selenium. I'm able to send the parameters to every input field except the price range input field. It is a dropdown menu. If I remove the price range, it is able to execute the script successfully. How can I select the element from the dropdown menu?

The error I'm facing is selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

I have tried so far,

from selenium import webdriver
import time

   
url = 'https://charliesmithrealty.net'

driver = webdriver.Chrome()
driver.set_window_size(1920, 1080)

driver.get(url)

# Zip Code
zip_code = driver.find_element_by_xpath("//input[@type='location']")
zip_code.send_keys("80023")
time.sleep(1)

# selectign zip code
select_zip_code = driver.find_element_by_xpath("//li[@lookup_field='zip_code']")
select_zip_code.click()
time.sleep(1)

# selecting price range
price_field = driver.find_element_by_xpath("//div[@class='bfg-dropdown']/button")
price_field.click()
time.sleep(1)

price_min = driver.find_element_by_xpath("(//div[@id='input-price']/div/ul/li[@data-val='250000'])[1]")
price_min.click()
time.sleep(3)

price_max = driver.find_element_by_xpath("(//div[@id='input-price']/div/ul/li[@data-val='250000'])[2]")
price_max.click()
time.sleep(3)


# selecting beds and baths
beds = driver.find_element_by_xpath("//div[@id='BedsDropdownMenuButton']/select/option[@value='4']")
beds.click()

baths = driver.find_element_by_xpath("//div[@id='BathsDropdownMenuButton']/select/option[@value='3']")
baths.click()
time.sleep(1)

# search
search_btn = driver.find_element_by_xpath("//button[@aria-label='Submit Search']")
search_btn.click()
time.sleep(3)

driver. Close()

CodePudding user response:

You can try the below locators:

# clicking on Price field 
driver.find_element(By.XPATH, "(.//*[@class='bfg-dropdown'])[1]").click()
time.sleep(0.2)
# to select min price - first value
driver.find_element(By.XPATH, "(.//*[contains(@class,'bfg-input-option-list bfg-option-list-min')])[2]/li[2]").click()
time.sleep(0.2)
# to select max price - first value
driver.find_element(By.XPATH, "(.//*[contains(@class,'bfg-input-option-list bfg-option-list-max')])[2]/li[1]").click()

The above code will select the first value in minimum and maximum price dropdown.

  • Related