1

I try to handle a dropdown menu to click on the 'Popular' option within this website using selenium, but no one example which I found doesn't suit for that.

    <select class="select__select--2gOcq explorerSortMenu__explorerSortPopoutMenu--3pMwT">
    <option value="desc__" selected="">Highest User Rating</option><option 
    value="desc__discount_percent">Discount</option><option value="asc__price">Price: Low to High</option><option value="desc__price">Price: High to Low</option><option value="desc__ratings_count">Popular</option></select>

Have used CSS, Xpath and Select, but the result is the same: No such element. Bellow you can see attempts and outputs.

Any ideas what I do wrong?

CSS Selector

browser.find_element_by_css_selector('.select__select--2gOcq.explorerSortMenu__explorerSortPopoutMenu--3pMwT')

Message: no such element: Unable to locate element: {"method":"css selector","selector":".select__select--2gOcq.explorerSortMenu__explorerSortPopoutMenu--3pMwT"}

Xpath

browser.find_element_by_xpath('//input[starts-with(@class,"select__select--2gOcq")]')

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[starts-with(@class,"select__select--2gOcq")]"}

Select

Select(browser.find_element_by_xpath("//*[@class='select__select--2gOcq explorerSortMenu__explorerSortPopoutMenu--3pMwT']"))

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='select__select--2gOcq explorerSortMenu__explorerSortPopoutMenu--3pMwT']"}

UPDATE:

After executed the code bellow the element was successfully located but, I have caught the TimeoutException.

driver = webdriver.Chrome()
driver.get(URL)
try:
    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[starts-with(@class, 'select__select--') and contains(@class, 'explorerSortMenu__explorerSortPopoutMenu--')]"))))
    select.select_by_visible_text('Popular')
    select.click()
finally:
    driver.quit()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
G_V
  • 39
  • 1
  • 8

2 Answers2

0

It would help to have the actual html but you could try using one of the multi-valued classes or even reversing the class order. Examples (these both work when testing on html sample):

.explorerSortMenu__explorerSortPopoutMenu--3pMwT.select__select--2gOcq

or

.select__select--2gOcq
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Yep, it work for the sample, but not tailored for real page. I sent the url above. Thanks – G_V Aug 26 '19 at 17:07
  • I sadly can't view that url. What error are you getting when running with actual page using selector as in my answer? – QHarr Aug 26 '19 at 17:09
  • The same issues: _No such element_. I suppose, the code that @DebanjanB provided is correct, but need to find out the right way to click() – G_V Aug 26 '19 at 17:44
  • does the selector find the element if you enter the selector in the search bar of the browser elements tab? F12 – QHarr Aug 26 '19 at 17:46
0

As the dropdown is based on <span> and <div> nodes so you can't use Select class and to click on the option Popular with in the website you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using XPATH:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_options = webdriver.ChromeOptions() 
    chrome_options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.vivino.com/explore?e=eJzLLbI11jNVy83MszU1MFDLTaywNTIAMpIrbT391JKBRJBaga2hWnqabVliUWZqSWKOWm6yrVp-EhDbpqQWJ6uVl0THAlWAKSMAxOAYsg==")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@class, 'responsiveDropdownMenu__title--')]//following::span[starts-with(@class, 'responsiveDropdownMenu__label--')]"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@class, 'responsiveDropdownMenu__menu--')]//a[@id='desc__ratings_count']"))).click()
    
  • Browser Snapshot:

vivino

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks, your code looks quite intuitive. I used `XPATH` that you got, but unfortunately the output for me is `TimeoutException`. `select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[starts-with(@class, 'select__select--') and contains(@class, 'explorerSortMenu__explorerSortPopoutMenu--')]"))))` `select.select_by_visible_text('Popular')` `select.click()` – G_V Aug 26 '19 at 16:53
  • @G_V Can you update the question with the exact error you are seeing as I have provided the code only to locate the select element, clicking an option will be next step. – undetected Selenium Aug 26 '19 at 16:59
  • @G_V Checkout the updated answer and let me know the status. – undetected Selenium Aug 26 '19 at 20:01