Yak Shaving

just me

WebDriver and Select Boxes

with one comment

This one had me puzzled for a while as I never took the time to sit down and read the documentation fully… I decided to look at this again after seeing the issue appear on the WebDriver mailing list. How to you use select and option html elements? Below is Python demo.

$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from webdriver_firefox.webdriver import FirefoxLauncher
>>> from webdriver_firefox.webdriver import WebDriver
>>> d = WebDriver()
>>> d.get("http://cassandra.appspot.com/")
>>> e = d.find_elements_by_xpath(
"/html/body/div[@id='container']/div[@id='search']/form[@id='searchForm']/div/select")
>>> r = e[0]
>>> t = r.find_elements_by_tag_name("option")
>>> t
[<webdriver_firefox.webelement.WebElement object at 0x8dd778c>,
<webdriver_firefox.webelement.WebElement object at 0x8dd772c>,
<webdriver_firefox.webelement.WebElement object at 0x8dd77ac>,
<webdriver_firefox.webelement.WebElement object at 0x8dd77ec>]
>>> for i in t:
...     print i.get_text()
...
Artist
Location
last.fm Username
Venue
>>> t[2].set_selected()
>>>

Now in your WebDriver browser session the option box has changed to “last.fm Username”. Excuse the variable names but I wanted to make a note before I lost the code.

Written by channam

September 6th, 2009 at 8:24 pm

Posted in python

One Response to 'WebDriver and Select Boxes'

Subscribe to comments with RSS or TrackBack to 'WebDriver and Select Boxes'.

  1. This DOM navigation is exactly the reason why we added “findElements” to the WebElement interface in the first place. Thank you for writing this up!

    Simon Stewart

    7 Sep 09 at 2:40 pm

Leave a Reply