Selenium 可以验证浏览器加载的 PDF 中的文本吗? [英] Can Selenium verify text inside a PDF loaded by the browser?

查看:17
本文介绍了Selenium 可以验证浏览器加载的 PDF 中的文本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Web 应用程序在浏览器中加载了 pdf.我已经想出了如何使用以下方法检查 pdf 是否已正确加载:

My web application loads a pdf in the browser. I have figured out how to check that the pdf has loaded correctly using:

验证属性xpath=//嵌入/@src{PDF 的网址在这里}

verifyAttribute xpath=//embed/@src {URL of PDF goes here}

能够使用 Selenium 检查 pdf 的内容真的很棒 - 例如验证某些文本是否存在.有没有办法做到这一点?

It would be really nice to be able to check the contents of the pdf with Selenium - for example verify that some text is present. Is there any way to do this?

推荐答案

虽然不受本机支持,但我找到了几种使用 java 驱动程序的方法.一种方法是在浏览器中打开 pdf(安装了 adobe acrobat),然后使用键盘快捷键选择所有文本(CTRL+A),然后将其复制到剪贴板(CTRL+C),然后您可以验证剪贴板中的文本.例如:

While not natively supported, I have found a couple ways using the java driver. One way is to have the pdf open in your browser (having adobe acrobat installed) and then use keyboard shortcut keys to select all text (CTRL+A), then copy it to the clipboard (CTRL+C) and then you can verify the text in the clipboard. eg:

protected String getLastWindow() {
    return session().getEval("var windowId; for(var x in selenium.browserbot.openedWindows ){windowId=x;} ");
}

@Test
public void testTextInPDF() {
    session().click("link=View PDF");
    String popupName = getLastWindow();
    session().waitForPopUp(popupName, PAGE_LOAD_TIMEOUT);
    session().selectWindow(popupName);

    session().windowMaximize();
    session().windowFocus();
    Thread.sleep(3000);

    session().keyDownNative("17"); // Stands for CTRL key
    session().keyPressNative("65"); // Stands for A "ascii code for A"
    session().keyUpNative("17"); //Releases CTRL key
    Thread.sleep(1000);

    session().keyDownNative("17"); // Stands for CTRL key
    session().keyPressNative("67"); // Stands for C "ascii code for C"
    session().keyUpNative("17"); //Releases CTRL key

    TextTransfer textTransfer = new TextTransfer();
    assertTrue(textTransfer.getClipboardContents().contains("Some text in my pdf"));
}

另一种方法,仍然在java中,是下载pdf,然后用PDFBox将pdf转换为文本,参见http://www.prasannatech.net/2009/01/convert-pdf-text-parser-java-api-pdfbox.html有关如何执行此操作的示例.

Another way, still in java, is to download the pdf and then convert the pdf to text with PDFBox, see http://www.prasannatech.net/2009/01/convert-pdf-text-parser-java-api-pdfbox.html for an example on how to do this.

这篇关于Selenium 可以验证浏览器加载的 PDF 中的文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆