2

first time posting!

I'm brand new to Python and Selenium, but I'm trying to automate a basic test and I can't find the answer to this problem.

On the main ebay.com page, I'm trying to choose the "All Categories" dropdown menu and choose the "Dolls & Bears" option (option value = "237"). When I execute my script, the menu is accessed, but the "Dolls & Bears" option does not get selected. The test does not return any errors. I have also tried using select_by_visible_text.

Here is my code. I appreciate any help!

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.ebay.com")
assert "Electronics" in driver.title
elem = driver.find_element_by_id("gh-ac")
elem.send_keys("funny bear")
driver.find_element_by_id("gh-cat").click()

def select_a_value(select):

Select.select_by_value("237").click()
0

1 Answer 1

4

Welcome to Stack Overflow!

You are very close with this code. "Select" is a class that can be instantiated, not just a library of functions. Here's a working version of your script:

from selenium import webdriver
from selenium.webdriver.support.select import Select
import time

driver = webdriver.Firefox()
driver.get("http://www.ebay.com")
assert "Electronics" in driver.title
elem = driver.find_element_by_id("gh-ac")
elem.send_keys("funny bear")
dropdown_web_element = driver.find_element_by_id("gh-cat")
select_box = Select(dropdown_web_element)
time.sleep(1)
select_box.select_by_value("237")

Notice that the call to Select passes in a value (a WebElement in this case) to instantiate the object. You can look at the selenium source code to figure out how it's called (Python27/Lib/site-packages/selenium/webdriver/support/select.py on Windows).

Also, I added a time.sleep(1) in there. You have stumbled on one of the frustrating things with selenium. Asynchronous loading of data on websites can lead to tests failing because they run faster than humans typically click buttons. The data might not be there when you get to that point in the script! The right way to deal with this is to dynamically wait a reasonable amount of time until the element you are looking for is there. A bit outside the scope of what you asked, but you will be dealing with that problem soon enough, I'm sure.

Good luck!

2
  • I just learned so much from your response, thank you! And yes, this works. The formatting on my modules didn't come out right in my post, but I was NOT using the 'import Select' module, so thank you. Also, I was unfamiliar with defining things like "dropdown_web_element" in your code example, but I see how it works. Do you know what this is called so I can learn about it? Thank you greatly!
    – staylucid
    Jun 24, 2014 at 3:52
  • dropdown_web_element was the variable name I used for clarity's sake. It could've been anything. driver.find_element_by_id("gh-cat") returns a reference to an element. If you are going to reference that element again, you need to name it something. In this case, we used dropdown_web_element.
    – Brian Mego
    Jun 24, 2014 at 14:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.