简体   繁体   中英

selecting a drop down item from a <div> menu selenium python

So I'm a little stuck! I'm trying to select an item from the 'All reviews' drop down however it doesn't interact like a where each item I could select the element of and then click it.

Instead the acts like an element where upon its label changing different results are displayed. Does anyone know how I could select an element from this menu?

For example, making the menu select the "Google" tab from the drop down.

for reference: https://www.google.com/maps/place/Hilton+London+Bankside/@51.5056536,-0.1033145,17z/data=!3m1!4b1!4m10!3m9!1s0x487604af6af74cc7:0x6c4cb3cbe03e95bf!5m2!4m1!1i2!8m2!3d51.5056536!4d-0.1011258!9m1!1b1

Induce WebDriverWait () and element_to_be_clickable () and click on the All reviews div element to open up the dropdown menu and then select the items based on text.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver=webdriver.Chrome()
driver.get("https://www.google.com/maps/place/Hilton+London+Bankside/@51.5056536,-0.1033145,17z/data=!3m1!4b1!4m10!3m9!1s0x487604af6af74cc7:0x6c4cb3cbe03e95bf!5m2!4m1!1i2!8m2!3d51.5056536!4d-0.1011258!9m1!1b1")
#Dropdown text provide here
selectItem='Agoda'
#First click on the All reviews element to open up the dorpdown element
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[aria-label='All reviews']"))).click()
#Select item from menu dropdown by text
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@role='menuitem']//div[text()='"+ selectItem +"']"))).click()

Browser snapshot: after execution

在此处输入图片说明

At this particular webpage elements are appearing,many of them don't just exist in the DOM content, you should use WebDriverWait method to wait until the specific element gets located.

For example, let's select "Google" as you asked:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("the google link here, it's too big to paste it")

#Waiting until dropdown is visible , there are two dropdowns, taking the first one
menu = WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.XPATH, " 
(//div[@class='cYrDcjyGO77__container'])[1]")))
menu.click()

#Waiting untill menu items is visible then selecting the second element - Google
item = WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.XPATH, " 
(//div[@role='menuitem'])[2]")))
item.click()

What you can do is to find your dropdown and then list thru all the options and select your one. Here is how I'm doing it

el = driver.find_element_by_id("dropdown_id")
for option in el.find_elements_by_tag_name('option'):
    if "GB" in option.text:
        option.click() # select() in earlier versions of webdriver
        break

I'm selecting the dropdown that have Value of the state "GB".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM