0

I went through a number very useful resources on Selenium parsing here, e.g. https://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver but could make my code work, so thought I would ask..

I'd like to pick up a dropdown option for example from ebay.com:

if I go to www.ebay.com and type "Minolta Rokkor", there will be a drop down menu, from which I want to pick, say, "minolta rokkor 40mm f/2".

I'm able to type in the query, but even getting the list of options isn't working for me. Would appreciate any help. Here's my code:

browser.get("https://www.ebay.com") time.sleep(6) e = browser.find_elements_by_class_name("gh-tb") e[0].send_keys("Minolta Rokkor") time.sleep(5) dropdown_web_element = browser.find_element_by_id("gh-ac") select_box = Select(dropdown_web_element) time.sleep(1) for o in select_box.options: print o.text 
2
  • It didn't give me a dropdown option when I type any words. Commented Jun 2, 2020 at 1:58
  • hmm, it definitely gives them to me - even with selenium Commented Jun 2, 2020 at 2:02

1 Answer 1

1

The problem is the "dropdown" is not actually a "select field". It may look like one. It may sort of function like one, but it isn't one.

enter image description here

The options are part of a ul that are styled to look like a select field.

You can use the following to obtain all options in the dropdown

browser.find_elements_by_xpath("//ul[contains(@class, 'ui-autocomplete')]//li/a") 

To click an option is a bit more tricky because the text isn't just inside the a tag, some of the text is surrounded by b tags, so doing so with xpath can be annoying. Here's what I came up with

browser.get("https://www.ebay.com") time.sleep(6) e = browser.find_elements_by_class_name("gh-tb") e[0].send_keys("Minolta Rokkor") time.sleep(5) dropdown_web_element = browser.find_element_by_id("gh-ac") select_options = browser.find_elements_by_xpath("//ul[contains(@class, 'ui-autocomplete')]//li/a") time.sleep(1) for ele in select_options: if ele.text == 'minolta rokkor 40mm f/2': click_id = ele.get_attribute('id') browser.find_element_by_id(click_id).click() 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was really helpful!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.