Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 99028

trying to get blob image from a url using selenium and java

$
0
0

I'm trying to download an image blob from a web page. Blob url looks like : blob:https://example.com/43bdcaf9-7d30-4b80-9306-7e411533b960

The image can be downloaded and converted to base64 text if I use javascript like below (in Chrome browser and running code as a browser extension):

async function blobToBase64(blobUrl) {    console.log(blobUrl);    const response = await fetch(blobUrl);    const blob = await response.blob();    const arrayBuffer = await blob.arrayBuffer();    const base64String = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));    return base64String;}

I need the same thing to be done in Java using Selenium Chrome driver, but not able to achieve it. I have tried below code:

                    String blob = "";                    if (driver instanceof JavascriptExecutor) {                        blob = ((JavascriptExecutor) driver).executeScript("const response = await fetch('" + baseUrl+"');" +"const blob = await response.blob();"+"const arrayBuffer = await blob.arrayBuffer();"+"const base64String = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));"+"return base64String;").toString();                        System.out.println(blob);                    }

but I get failed to fetch url error.Please help to fetch the image blob and convert to base64 String

org.openqa.selenium.JavascriptException: javascript error: Failed to fetch  (Session info: chrome=130.0.6723.91)Build info: version: '4.18.1', revision: 'b1d3319b48'System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '23'Driver info: org.openqa.selenium.remote.RemoteWebDriver

Thanks in advance.


Viewing all articles
Browse latest Browse all 99028

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>