“There is no real difference between work and play – it's all living.”

-Richard Branson

Revealing Class Documentation in Python

Today I was using Selenium’s Python Client Driver for some automated data-collection tasks.

The Selenium documentation includes a nice example of simulating user input for filling in forms.

WebDriver’s Select class Source Article
1
2
3
4
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_tag_name("select"))
select.deselect_all()
select.select_by_visible_text("Edam")

But I couldn’t find any documentation on the Select class to see what else it can do! Fortunately, Python’s help function can show the source documentation of the class for the object you pass it.

Example of using help(object)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> select_element = Select(driver.find_element_by_tag_name('select'))
>>> help(select_element)
Help on instance of Select in module selenium.webdriver.support.select:

class Select
 |  Methods defined here:
 |
 |  __init__(self, webelement)
 |      Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not,
 |      then an UnexpectedTagNameException is thrown.
 |
 |      :Args:
 |       - webelement - element SELECT element to wrap
 |
 |      Example:
 |          from selenium.webdriver.support.ui import Select
 |
 |          Select(driver.find_element_by_tag_name("select")).select_by_index(2)
 |
 |  deselect_all(self)
 |      Clear all selected entries. This is only valid when the SELECT supports multiple selections.
 |      throws NotImplementedError If the SELECT does not support multiple selections
 |
:

Lucky me, the class contains detailed docstrings! That makes me a happy hacker.

If you just want to see all the method names, you can use dir(object).

Example of dir(object)
1
2
>>> dir(select_element)
['__doc__', '__init__', '__module__', '_el', '_escapeString', '_get_longest_token', '_setSelected', '_unsetSelected', 'all_selected_options', 'deselect_all', 'deselect_by_index', 'deselect_by_value', 'deselect_by_visible_text', 'first_selected_option', 'is_multiple', 'options', 'select_by_index', 'select_by_value', 'select_by_visible_text']