Skip to content

Instantly share code, notes, and snippets.

@Donavan
Created March 19, 2021 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Donavan/1f349cfffe43b8c625966d98c12247df to your computer and use it in GitHub Desktop.
Save Donavan/1f349cfffe43b8c625966d98c12247df to your computer and use it in GitHub Desktop.
Dealing with blob URLs for images in automation
# This code can be executed in selenium via a javascript executor. It will change the source of an image that uses a blob to a data url instead
# You will need to supply the ID of the image tag, or change to code to look it up some other way.
# This line needs changed / passed in
var image = document.getElementById(YOUR_IMAGE_ID);
var blobUrl = image.src;
var xhr = new XMLHttpRequest;
xhr.responseType = 'blob';
xhr.onload = function() {
var recoveredBlob = xhr.response;
var reader = new FileReader;
reader.onload = function() {
var blobAsDataUrl = reader.result;
image.src = blobAsDataUrl;
};
reader.readAsDataURL(recoveredBlob);
};
xhr.open('GET', blobUrl);
xhr.send()
# This code can be executed in selenium via a javascript executor. It will return a URL that can be downloaded to a file.
# You will need to supply the blobl url for the image below
# This line needs changed / passed in
var blobUrl = IMAGE_BLOB_URL_HERE;
var response = await new Promise(resolve => {
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.open("GET", blobUrl);
xhr.onload = function(e) {
var recoveredBlob = xhr.response;
var reader = new FileReader;
reader.onload = function() {
var blobAsDataUrl = reader.result;
resolve(blobAsDataUrl);
};
reader.readAsDataURL(recoveredBlob);
};
xhr.onerror = function () {
resolve(undefined);
console.error("** An error occurred during the XMLHttpRequest");
};
xhr.send();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment