I'm trying to print the options and select "Manhattan" from the "city" dropdown search box on this website: https://upxland.me/properties/ . But every time I run it, the program end without printing anything. In addition, could anyone show me how to type part of a city's name (like "Manha") and then select from the dropdown list?
My code is shown below. Could anyone help?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
import time
PATH = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://upxland.me/properties/")
try:
city = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'input-74')))
# city.send_keys("o")
# city.send_keys(Keys.RETURN)
city_selection = Select(city)
# print the number of option
print(len(city_selection.options))
# print all options
for option in city_selection.options:
print(option.text)
# select by index
city_selection.select_by_index(3)
time.sleep(3)
# select by value
# city_selection.select_by_value()
except:
driver.quit()
CodePudding user response:
You need to click and enter some text in the input box for city. Then add 1 sec wait. Then use the options xpath to get a list of elements.
Get text for each element by Iterating through the list till you get the required city and then click on it.
The xpath for the select element is -
//input[@placeholder="City"]
The xpath for the options for select element is -
//div[contains(@class,'content__active v-autocomplete__content')]//div[@class='v-list-item__title']
CodePudding user response:
The desired element aren't within any html-select tag, but they are within <div>
tag.
So you won't be able to use Select() class.
Solution
To print the option texts you can use List Comprehension and you can use the following locator strategies:
Code block:
driver.execute("get", {'url': 'https://upxland.me/properties/'}) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept all']"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[text()='City']//following-sibling::input[1]"))).click() print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='v-menu__content theme--dark menuable__content__active v-autocomplete__content']//div[@role='option']//div[@class='v-list-item__title']")))])
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Console output:
['Los Angeles', 'San Francisco', 'Manhattan', 'Queens', 'Fresno', 'Brooklyn', 'Oakland', 'Staten Island', 'Bakersfield', 'Chicago', 'Cleveland', 'Santa Clara', 'Rutherford', 'Kansas City', 'New Orleans', 'Nashville', 'Bronx', 'Detroit', 'Las Vegas']
To select Manhattan:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='v-menu__content theme--dark menuable__content__active v-autocomplete__content']//div[@role='option']//div[@class='v-list-item__title' and text()='Manhattan']"))).click()