Skip to content

Using Select Boxes with WebDriver

This is a little update to my previous WebDriver post. The code below allows you to set the content of select boxes by accessing the element directly, in this case using XPath to find the element.

#!/usr/bin/env python

from webdriver_firefox.webdriver import FirefoxLauncher
from webdriver_firefox.webdriver import WebDriver

driver = WebDriver()
driver.get("http://cassandra.appspot.com/")

elements = driver.find_elements_by_xpath(
"/html/body/div[@id='container']/div[@id='search']/form[@id='searchForm']/div/select")

select_box = elements[0]
options = select_box.find_elements_by_tag_name("option")

for option in options:
    print option.get_text()

# sets the select box to last.fm Username
options[2].set_selected()