【问题标题】:How to access the PDF file having blob protocol, java.net.MalformedURLException: unknown protocol: blob how to resolve in selenium java?如何访问具有blob协议的PDF文件,java.net.MalformedURLException:未知协议:blob如何在selenium java中解决?
【发布时间】:2022-08-05 14:01:22
【问题描述】:

如何访问具有blob协议的PDF文件,java.net.MalformedURLException:未知协议:blob如何在selenium java中解决?

  • 这似乎不是一个问题。如果你想提供一些关于如何做某事的信息,你仍然必须提出一个问题,然后你可以写下你自己的答案。
  • 谢谢,有时间我会更新的

标签: java automation


【解决方案1】:

我找到了如何访问具有 blob 协议的 PDF 文件 java.net.MalformedURLException: unknown protocol: blob how to resolve in selenium java? 问题的解决方案。

  1. 首先使用以下代码将 blob 链接上的 PDF 转换为 base64:
      private String getBytesBase64FromBlobURI(ChromeDriver 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.onerror = function(){ callback(xhr.status) };"
                    + "xhr.open('GET','"+ uri +"');"
                    + "xhr.send();";
            String result = (String) driver.executeAsyncScript(script, uri);
            return result;
        }
    
    1. 第二步现在将base64转换为pdf文件
    public void savePDF(String path, String x){
    File file = new File(path);//"D:\\dealer-portal-v2-automation\\test.pdf");
    
            //Converting base64 to PDF
            try (FileOutputStream fos = new FileOutputStream(file); ) {
                // To be short I use a corrupted PDF string, so make sure to use a valid one if you want to preview the PDF file
                byte[] decoder = Base64.getDecoder().decode(x);
                fos.write(decoder);
                System.out.println("PDF File Saved");
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
    
    1. 现在是主类
    public void main(Strings[] args) {
    //here pass your driver and the url 
            String x = getBytesBase64FromBlobURI((ChromeDriver) driver,url);
    //here convert your base64 to file and sendt base64 string and the path where you want to //store your PDF
            savePDF(path, x);
    }
    

【讨论】:

    猜你喜欢
    • 2014-05-24
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    相关资源
    最近更新 更多