Skip to content

Commit 2294fbb

Browse files
Artur Orlovlukeis
authored andcommitted
File upload using remotedriver on python3
remove comment Fixes Issue #6167
1 parent a50f7ba commit 2294fbb

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

py/selenium/webdriver/remote/webelement.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
import os
1919
import zipfile
2020
try:
21-
from StringIO import StringIO
21+
from StringIO import StringIO as IOStream
2222
except ImportError: # 3+
23-
from io import StringIO
23+
from io import BytesIO as IOStream
2424
import base64
2525

2626

2727
from .command import Command
28-
from selenium.common.exceptions import WebDriverException
28+
from selenium.common.exceptions import WebDriverException
2929
from selenium.common.exceptions import InvalidSelectorException
3030
from selenium.webdriver.common.by import By
3131
from selenium.webdriver.common.keys import Keys
@@ -69,7 +69,7 @@ def clear(self):
6969

7070
def get_attribute(self, name):
7171
"""Gets the attribute value.
72-
72+
7373
:Args:
7474
- name - name of the attribute property to retieve.
7575
@@ -103,16 +103,16 @@ def is_enabled(self):
103103

104104
def find_element_by_id(self, id_):
105105
"""Finds element within the child elements of this element.
106-
106+
107107
:Args:
108108
- id_ - ID of child element to locate.
109109
"""
110110
return self.find_element(by=By.ID, value=id_)
111111

112112
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
114114
with the matching ID.
115-
115+
116116
:Args:
117117
- id_ - Id of child element to find.
118118
"""
@@ -127,63 +127,63 @@ def find_element_by_name(self, name):
127127

128128
def find_elements_by_name(self, name):
129129
"""Finds a list of elements with in this element's children by name.
130-
130+
131131
:Args:
132132
- name - name property to search for.
133133
"""
134134
return self.find_elements(by=By.NAME, value=name)
135135

136136
def find_element_by_link_text(self, link_text):
137137
"""Finds element with in this element's children by visible link text.
138-
138+
139139
:Args:
140140
- link_text - Link text string to search for.
141141
"""
142142
return self.find_element(by=By.LINK_TEXT, value=link_text)
143143

144144
def find_elements_by_link_text(self, link_text):
145145
"""Finds a list of elements with in this element's children by visible link text.
146-
146+
147147
:Args:
148148
- link_text - Link text string to search for.
149149
"""
150150
return self.find_elements(by=By.LINK_TEXT, value=link_text)
151151

152152
def find_element_by_partial_link_text(self, link_text):
153153
"""Finds element with in this element's children by parial visible link text.
154-
154+
155155
:Args:
156156
- link_text - Link text string to search for.
157157
"""
158158
return self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)
159159

160160
def find_elements_by_partial_link_text(self, link_text):
161161
"""Finds a list of elements with in this element's children by link text.
162-
162+
163163
:Args:
164164
- link_text - Link text string to search for.
165165
"""
166166
return self.find_elements(by=By.PARTIAL_LINK_TEXT, value=link_text)
167167

168168
def find_element_by_tag_name(self, name):
169169
"""Finds element with in this element's children by tag name.
170-
170+
171171
:Args:
172172
- name - name of html tag (eg: h1, a, span)
173173
"""
174174
return self.find_element(by=By.TAG_NAME, value=name)
175175

176176
def find_elements_by_tag_name(self, name):
177177
"""Finds a list of elements with in this element's children by tag name.
178-
178+
179179
:Args:
180180
- name - name of html tag (eg: h1, a, span)
181181
"""
182182
return self.find_elements(by=By.TAG_NAME, value=name)
183183

184184
def find_element_by_xpath(self, xpath):
185185
"""Finds element by xpath.
186-
186+
187187
:Args:
188188
xpath - xpath of element to locate. "//input[@class='myelement']"
189189
@@ -202,7 +202,7 @@ def find_element_by_xpath(self, xpath):
202202

203203
def find_elements_by_xpath(self, xpath):
204204
"""Finds elements within the elements by xpath.
205-
205+
206206
:Args:
207207
- xpath - xpath locator string.
208208
@@ -220,32 +220,32 @@ def find_elements_by_xpath(self, xpath):
220220

221221
def find_element_by_class_name(self, name):
222222
"""Finds an element within this element's children by their class name.
223-
223+
224224
:Args:
225225
- name - class name to search on.
226226
"""
227227
return self.find_element(by=By.CLASS_NAME, value=name)
228228

229229
def find_elements_by_class_name(self, name):
230230
"""Finds a list of elements within children of this element by their class name.
231-
231+
232232
:Args:
233233
- name - class name to search on.
234234
"""
235235
return self.find_elements(by=By.CLASS_NAME, value=name)
236236

237237
def find_element_by_css_selector(self, css_selector):
238238
"""Find and return an element that's a child of this element by CSS selector.
239-
239+
240240
:Args:
241241
- css_selector - CSS selctor string, ex: 'a.nav#home'
242242
"""
243243
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
244244

245245
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
247247
element by CSS selector.
248-
248+
249249
:Args:
250250
- css_selector - CSS selctor string, ex: 'a.nav#home'
251251
"""
@@ -255,19 +255,19 @@ def send_keys(self, *value):
255255
"""Simulates typing into the element.
256256
257257
: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
259259
file inputs, this could be a local file path.
260260
261261
Use this to send simple key events or to fill out form fields::
262262
263263
form_textfield = driver.find_element_by_name('username')
264264
form_textfield.send_keys("admin")
265-
265+
266266
This can also be used to set file inputs.::
267267
268268
file_input = driver.find_element_by_name('profilePic')
269269
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
271271
# in os.path to return the actual path to support cross OS testing.
272272
# file_input.send_keys(os.path.abspath("path/to/profilepic.gif"))
273273
@@ -336,11 +336,11 @@ def parent(self):
336336

337337
@property
338338
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
342342
refer to the same element, can be done using '=='::
343-
343+
344344
if element1 == element2:
345345
print("These 2 are equal")
346346
@@ -387,13 +387,16 @@ def __hash__(self):
387387
return int(hashlib.md5(self._id.encode('utf-8')).hexdigest(), 16)
388388

389389
def _upload(self, filename):
390-
fp = StringIO()
390+
fp = IOStream()
391391
zipped = zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED)
392392
zipped.write(filename, os.path.split(filename)[1])
393393
zipped.close()
394+
content = base64.encodestring(fp.getvalue())
395+
if not isinstance(content, str):
396+
content = content.decode('utf-8')
394397
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']
397400
except WebDriverException as e:
398401
if "Unrecognized command: POST" in e.__str__():
399402
return filename

0 commit comments

Comments
 (0)