How to read a Blob:Https url via Selenium
I am trying to use the below code for reading a blob file, but it throws error saying Protocol not supported. There was a suggestion to convert the URL to Base 64 (used getBytesBase64FromBlobURI function but again derived it from stackoverflow) which did processed but the new FinalUrl delivered from it returns 0, hence no luck with parsing the file.
Any workaround solution for the same?
public ReadPDF read() throws IOException { this.driver.get("blob:https://x-company.com/6ad93a15-f711-4db7-98f0-c0c81a632e5c"); String currentURL = driver.getCurrentUrl(); URL url = new URL(currentURL); //String FinalURL = getBytesBase64FromBlobURI(getDriver(), currentURL); //URL url = new URL(FinalURL); InputStream is = url.openStream(); BufferedInputStream fileParse = new BufferedInputStream(is); PDDocument document; document = PDDocument.load(fileParse); String pdfContent = new PDFTextStripper().getText(document); System.out.println(pdfContent); return this; } private String getBytesBase64FromBlobURI(Driver driver, String uri) { String script = " " + "var uri = arguments[0];" + "var callback = arguments[1];" + "var toBase64 = function(buffer){for(var r,n=new Uint8Array(buffer),t=n.length,a=new Uint8Array(4*Math.ceil(t/3)),i=new Uint8Array(64),o=0,c=0;64>c;++c)i[c]='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.charCodeAt(c);for(c=0;t-t%3>c;c+=3,o+=4)r=n[c]<<16|n[c+1]<<8|n[c+2],a[o]=i[r>>18],a[o+1]=i[r>>12&63],a[o+2]=i[r>>6&63],a[o+3]=i[63&r];return t%3===1?(r=n[t-1],a[o]=i[r>>2],a[o+1]=i[r<<4&63],a[o+2]=61,a[o+3]=61):t%3===2&&(r=(n[t-2]<<8)+n[t-1],a[o]=i[r>>10],a[o+1]=i[r>>4&63],a[o+2]=i[r<<2&63],a[o+3]=61),new TextDecoder('ascii').decode(a)};" + "var xhr = new XMLHttpRequest();" + "xhr.responseType = 'arraybuffer';" + "xhr.onload = function(){ callback(toBase64(xhr.response)) };" // + "xhr.responseType = 'blob';" // + "xhr.onload = function(e) { if (this.status == 200) { var myBlob = this.response; } };" + "xhr.onerror = function(){ callback(xhr.status) };" + "xhr.open('GET','"+ uri +"');" + "xhr.send();"; System.out.println(driver.executeAsyncScript(script, uri)); String result = driver.executeAsyncScript(script, uri).toString(); System.out.println("Final URL: >>>> " + result); return result; }
P.S: there is similar question asked but i dont see concrete answer on the same.
https://forum.katalon.com/t/read-pdf-having-url-starting-with-blob/24095/11