0

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

3 Answers 3

2

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

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, I've included my answer as well with no hard coding. I'm going to test yours too!
1

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

Comments

1

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.