Created
December 11, 2020 20:41
-
-
Save hellpanderrr/4300186c22b64bb2ef5e08c3f261a290 to your computer and use it in GitHub Desktop.
python selenium save image as base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
url = 'https://www.gravatar.com/avatar/ad45f075aa4d69cbd552b671a32bf3ed?s=32&d=identicon&r=PG&f=1' | |
def save_image_as_base64(browser, url): | |
""" | |
""" | |
base = browser.execute_async_script(""" | |
var done = arguments[0]; | |
function toDataURL(url, callback) { | |
var xhr = new XMLHttpRequest(); | |
xhr.onload = function() { | |
var reader = new FileReader(); | |
reader.onloadend = function() { | |
callback(reader.result); | |
} | |
reader.readAsDataURL(xhr.response); | |
}; | |
xhr.open('GET', url); | |
xhr.responseType = 'blob'; | |
xhr.send(); | |
} | |
toDataURL('%s', done)""" % url) | |
return base | |
save_image_as_base64(browser, url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment