18
18
import os
19
19
import zipfile
20
20
try :
21
- from StringIO import StringIO
21
+ from StringIO import StringIO as IOStream
22
22
except ImportError : # 3+
23
- from io import StringIO
23
+ from io import BytesIO as IOStream
24
24
import base64
25
25
26
26
27
27
from .command import Command
28
- from selenium .common .exceptions import WebDriverException
28
+ from selenium .common .exceptions import WebDriverException
29
29
from selenium .common .exceptions import InvalidSelectorException
30
30
from selenium .webdriver .common .by import By
31
31
from selenium .webdriver .common .keys import Keys
@@ -69,7 +69,7 @@ def clear(self):
69
69
70
70
def get_attribute (self , name ):
71
71
"""Gets the attribute value.
72
-
72
+
73
73
:Args:
74
74
- name - name of the attribute property to retieve.
75
75
@@ -103,16 +103,16 @@ def is_enabled(self):
103
103
104
104
def find_element_by_id (self , id_ ):
105
105
"""Finds element within the child elements of this element.
106
-
106
+
107
107
:Args:
108
108
- id_ - ID of child element to locate.
109
109
"""
110
110
return self .find_element (by = By .ID , value = id_ )
111
111
112
112
def find_elements_by_id (self , id_ ):
113
- """Finds a list of elements within the children of this element
113
+ """Finds a list of elements within the children of this element
114
114
with the matching ID.
115
-
115
+
116
116
:Args:
117
117
- id_ - Id of child element to find.
118
118
"""
@@ -127,63 +127,63 @@ def find_element_by_name(self, name):
127
127
128
128
def find_elements_by_name (self , name ):
129
129
"""Finds a list of elements with in this element's children by name.
130
-
130
+
131
131
:Args:
132
132
- name - name property to search for.
133
133
"""
134
134
return self .find_elements (by = By .NAME , value = name )
135
135
136
136
def find_element_by_link_text (self , link_text ):
137
137
"""Finds element with in this element's children by visible link text.
138
-
138
+
139
139
:Args:
140
140
- link_text - Link text string to search for.
141
141
"""
142
142
return self .find_element (by = By .LINK_TEXT , value = link_text )
143
143
144
144
def find_elements_by_link_text (self , link_text ):
145
145
"""Finds a list of elements with in this element's children by visible link text.
146
-
146
+
147
147
:Args:
148
148
- link_text - Link text string to search for.
149
149
"""
150
150
return self .find_elements (by = By .LINK_TEXT , value = link_text )
151
151
152
152
def find_element_by_partial_link_text (self , link_text ):
153
153
"""Finds element with in this element's children by parial visible link text.
154
-
154
+
155
155
:Args:
156
156
- link_text - Link text string to search for.
157
157
"""
158
158
return self .find_element (by = By .PARTIAL_LINK_TEXT , value = link_text )
159
159
160
160
def find_elements_by_partial_link_text (self , link_text ):
161
161
"""Finds a list of elements with in this element's children by link text.
162
-
162
+
163
163
:Args:
164
164
- link_text - Link text string to search for.
165
165
"""
166
166
return self .find_elements (by = By .PARTIAL_LINK_TEXT , value = link_text )
167
167
168
168
def find_element_by_tag_name (self , name ):
169
169
"""Finds element with in this element's children by tag name.
170
-
170
+
171
171
:Args:
172
172
- name - name of html tag (eg: h1, a, span)
173
173
"""
174
174
return self .find_element (by = By .TAG_NAME , value = name )
175
175
176
176
def find_elements_by_tag_name (self , name ):
177
177
"""Finds a list of elements with in this element's children by tag name.
178
-
178
+
179
179
:Args:
180
180
- name - name of html tag (eg: h1, a, span)
181
181
"""
182
182
return self .find_elements (by = By .TAG_NAME , value = name )
183
183
184
184
def find_element_by_xpath (self , xpath ):
185
185
"""Finds element by xpath.
186
-
186
+
187
187
:Args:
188
188
xpath - xpath of element to locate. "//input[@class='myelement']"
189
189
@@ -202,7 +202,7 @@ def find_element_by_xpath(self, xpath):
202
202
203
203
def find_elements_by_xpath (self , xpath ):
204
204
"""Finds elements within the elements by xpath.
205
-
205
+
206
206
:Args:
207
207
- xpath - xpath locator string.
208
208
@@ -220,32 +220,32 @@ def find_elements_by_xpath(self, xpath):
220
220
221
221
def find_element_by_class_name (self , name ):
222
222
"""Finds an element within this element's children by their class name.
223
-
223
+
224
224
:Args:
225
225
- name - class name to search on.
226
226
"""
227
227
return self .find_element (by = By .CLASS_NAME , value = name )
228
228
229
229
def find_elements_by_class_name (self , name ):
230
230
"""Finds a list of elements within children of this element by their class name.
231
-
231
+
232
232
:Args:
233
233
- name - class name to search on.
234
234
"""
235
235
return self .find_elements (by = By .CLASS_NAME , value = name )
236
236
237
237
def find_element_by_css_selector (self , css_selector ):
238
238
"""Find and return an element that's a child of this element by CSS selector.
239
-
239
+
240
240
:Args:
241
241
- css_selector - CSS selctor string, ex: 'a.nav#home'
242
242
"""
243
243
return self .find_element (by = By .CSS_SELECTOR , value = css_selector )
244
244
245
245
def find_elements_by_css_selector (self , css_selector ):
246
- """Find and return list of multiple elements within the children of this
246
+ """Find and return list of multiple elements within the children of this
247
247
element by CSS selector.
248
-
248
+
249
249
:Args:
250
250
- css_selector - CSS selctor string, ex: 'a.nav#home'
251
251
"""
@@ -255,19 +255,19 @@ def send_keys(self, *value):
255
255
"""Simulates typing into the element.
256
256
257
257
:Args:
258
- - value - A string for typing, or setting form fields. For setting
258
+ - value - A string for typing, or setting form fields. For setting
259
259
file inputs, this could be a local file path.
260
260
261
261
Use this to send simple key events or to fill out form fields::
262
262
263
263
form_textfield = driver.find_element_by_name('username')
264
264
form_textfield.send_keys("admin")
265
-
265
+
266
266
This can also be used to set file inputs.::
267
267
268
268
file_input = driver.find_element_by_name('profilePic')
269
269
file_input.send_keys("path/to/profilepic.gif")
270
- # Generally it's better to wrap the file path in one of the methods
270
+ # Generally it's better to wrap the file path in one of the methods
271
271
# in os.path to return the actual path to support cross OS testing.
272
272
# file_input.send_keys(os.path.abspath("path/to/profilepic.gif"))
273
273
@@ -336,11 +336,11 @@ def parent(self):
336
336
337
337
@property
338
338
def id (self ):
339
- """ Returns internal id used by selenium.
340
-
341
- This is mainly for internal use. Simple use cases such as checking if 2 webelements
339
+ """ Returns internal id used by selenium.
340
+
341
+ This is mainly for internal use. Simple use cases such as checking if 2 webelements
342
342
refer to the same element, can be done using '=='::
343
-
343
+
344
344
if element1 == element2:
345
345
print("These 2 are equal")
346
346
@@ -387,13 +387,16 @@ def __hash__(self):
387
387
return int (hashlib .md5 (self ._id .encode ('utf-8' )).hexdigest (), 16 )
388
388
389
389
def _upload (self , filename ):
390
- fp = StringIO ()
390
+ fp = IOStream ()
391
391
zipped = zipfile .ZipFile (fp , 'w' , zipfile .ZIP_DEFLATED )
392
392
zipped .write (filename , os .path .split (filename )[1 ])
393
393
zipped .close ()
394
+ content = base64 .encodestring (fp .getvalue ())
395
+ if not isinstance (content , str ):
396
+ content = content .decode ('utf-8' )
394
397
try :
395
- return self ._execute (Command .UPLOAD_FILE ,
396
- {'file' : base64 . encodestring ( fp . getvalue ()) })['value' ]
398
+ return self ._execute (Command .UPLOAD_FILE ,
399
+ {'file' : content })['value' ]
397
400
except WebDriverException as e :
398
401
if "Unrecognized command: POST" in e .__str__ ():
399
402
return filename
0 commit comments