Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix base64 encoded string #231

Merged
merged 1 commit into from
Jul 13, 2018
Merged

Conversation

KazuCocoa
Copy link
Member

Fix: function find_element_by_image always raise an exception: "TypeError: Object of type 'bytes' is not JSON serializable" #228

encodestring generate byte data in Python 3. Thus, we should encode it to string.

Python2.7

>>> import base64
>>> base64.encodestring(b'hello')
'aGVsbG8=\n'

Python3.6

>>> import base64
>>> base64.encodestring(b'hello')
b'aGVsbG8=\n'

b64_data should be base64 encoded string.
This way is as same as https://github.com/SeleniumHQ/selenium/blob/6b5fe890da7d1592693d1dfcf795d882fc0426c5/py/selenium/webdriver/firefox/firefox_profile.py#L180 way.

@@ -355,7 +355,7 @@ def find_element_by_image(self, png_img_path,
"""
screenshot = self.get_screenshot_as_base64()
with open(png_img_path, 'rb') as png_file:
b64_data = base64.encodestring(png_file.read())
b64_data = base64.b64encode(png_file.read()).decode('UTF-8')
Copy link
Member Author

@KazuCocoa KazuCocoa Jul 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could call json.dump(b64_data) without exception in Python2/3

with open('/Users/kazu/GitHub/python-client/sample.JPG', 'rb') as f:
    b64_data = base64. b64encode(f.read()).decode('UTF-8')

json.dumps(b64_data)

@KazuCocoa KazuCocoa merged commit c6f58bc into appium:master Jul 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants