由于未知协议(protocol) : blob,java.net.URL 类抛出 MalformedException

标签 java selenium url automation pdf-parsing

我正在自动化我的测试场景以验证 pdf 文档。单击文档链接( anchor 标记)后,该文档将在新的浏览器选项卡中打开。我想验证我使用 Apache PDFBox 的文档中的一些重要内容。但是,文档 URL 有一个前缀“blob”,因此 java.net.URL 类会针对未知协议(protocol)抛出 MalformedException:blob。我应该如何在java中定义/添加该协议(protocol)?

请让我知道如何消除此错误,以便我可以成功使用 PDFBox 解析我的 pdf 文件。

Java 版本 - 1.8

这是 pdf 文档在浏览器中打开后的屏幕截图。 enter image description here

这是文档的 HTML 源代码。但是,由于它是 pdf View ,因此无法执行任何操作,例如获取文本/windowTitle 等。 enter image description here

以下是示例代码片段 -

public void readPdfContents() throws IOException {

    String url = "blob:https://cpswebqa.testcbidata.com/f9ad63bc-700e-4f49-a4fb-807ad1a44b01";
    URL pdfUrl = new URL(url);
    InputStream ips = pdfUrl.openStream();
    BufferedInputStream bis = new BufferedInputStream(ips);
    PDFParser pdfParser = new PDFParser(bis);
    pdfParser.parse();
    String pdfData = new PDFTextStripper().getText(pdfParser.getPDDocument());

    System.out.println("PDF Data is - " + pdfData);

}

错误堆栈跟踪 -

Exception in thread "main" java.net.MalformedURLException: unknown protocol: blob
    at java.net.URL.<init>(URL.java:600)
    at java.net.URL.<init>(URL.java:490)
    at java.net.URL.<init>(URL.java:439)
    at com.cbsh.automation.file.testrunner.WEB.Sample.main(Sample.java:11)

最佳答案

我遇到了同样的问题,并找到了一个注入(inject) Javascript 的解决方案,如下所示:

How to download an image with Python 3/Selenium if the URL begins with “blob:”?

我用Java编写的,效果很好,代码如下:

 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;
}

我希望它对某人有帮助。

干杯!

关于由于未知协议(protocol) : blob,java.net.URL 类抛出 MalformedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59313902/

相关文章:

java - 使用 POI 在 Java 中的 Excel 中将图像添加到标题右侧

JavaFX 在拖放过程中获取鼠标事件

python - 由于可见性问题,Selenium 将不会选择下拉选项

url - OpenCart seo_url 从 url 中删除 product_id

java - Android - 将 ImageView 设置为 URL

java - 每次单击时创建 JLabel 的新实例

java - 列出类路径中的所有属性文件并在java中读取它们

c# - 如何使用 C# 在 Selenium WebDriver (Selenium 2) 中最大化浏览器窗口?

java - 使用 selenium/java 生成随机字符并发送到文本字段

AngularJS Codeigniter base_url 路径设置