@@ -74,11 +74,11 @@ Filling in forms
74
74
75
75
We've already seen how to enter text into a textarea or text field,
76
76
but what about the other elements? You can "toggle" the state of
77
- checkboxes , and you can use "setSelected" to set something like an
77
+ drop down , and you can use "setSelected" to set something like an
78
78
`OPTION ` tag selected. Dealing with `SELECT ` tags isn't too bad::
79
79
80
- select = driver.find_element_by_xpath("//select")
81
- all_options = select .find_elements_by_tag_name("option")
80
+ element = driver.find_element_by_xpath("//select[@name='name'] ")
81
+ all_options = element .find_elements_by_tag_name("option")
82
82
for option in all_options:
83
83
print "Value is: %s" % option.get_attribute("value")
84
84
option.click()
@@ -87,19 +87,31 @@ This will find the first "SELECT" element on the page, and cycle
87
87
through each of it's OPTIONs in turn, printing out their values, and
88
88
selecting each in turn.
89
89
90
- .. As you can see, this isn't the most efficient
91
- .. way of dealing with SELECT elements . WebDriver's support classes
92
- .. include one called "Select", which provides useful methods for
93
- .. interacting with these.
90
+ As you can see, this isn't the most efficient
91
+ way of dealing with SELECT elements . WebDriver's support classes
92
+ include one called "Select", which provides useful methods for
93
+ interacting with these.
94
94
95
- .. ::
95
+ ::
96
+
97
+ select = Select(driver.find_element_by_name('name'))
98
+ select.select_by_index(index)
99
+ select.select_by_visible_text("text")
100
+ select.select_by_value(value)
101
+
102
+
103
+ WebDriver also provides features for deselecting all the selected options::
96
104
97
- .. select = driver.find_element_by_xpath("//select").select() #<- FIXME: API
98
- .. select.deselectAll() #<- FIXME: API
99
- .. select.selectByVisibleText("Edam") #<- FIXME: API
105
+ select = Select(driver.find_element_by_id('id'))
106
+ select.deselect_all()
100
107
101
- .. This will deselect all OPTIONs from the first SELECT on the page, and
102
- .. then select the OPTION with the displayed text of "Edam".
108
+ This will deselect all OPTIONs from the first SELECT on the page.
109
+
110
+ Suppose in a test, we need the list of all default selected options, Select
111
+ class provides a method that returns a list::
112
+
113
+ select = Select(driver.find_element_by_xpath("xpath"))
114
+ all_options = select.all_selected_options()
103
115
104
116
Once you've finished filling out the form, you probably want to submit
105
117
it. One way to do this would be to find the "submit" button and click
@@ -165,6 +177,10 @@ and you can specify the frame by its index too. That is::
165
177
would go to the frame named "child" of the first subframe of the frame
166
178
called "frameName". **All frames are evaluated as if from *top*. **
167
179
180
+ Once we are done with working on frames, we will have to come back
181
+ to the parent frame which can be done using::
182
+
183
+ driver.switch_to_default_content()
168
184
169
185
Popup dialogs
170
186
~~~~~~~~~~~~~
0 commit comments