Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

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

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

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?

1 Answer

0 votes
by (62.9k points)

While not natively supported, I have found a couple of 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;} ");



public void testTextInPDF() {

    session().click("link=View PDF");

    String popupName = getLastWindow();

    session().waitForPopUp(popupName, PAGE_LOAD_TIMEOUT);





    session().keyDownNative("17"); // Stands for CTRL key

    session().keyPressNative("65"); // Stands for A "ascii code for A"

    session().keyUpNative("17"); //Releases CTRL key


    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"));


Another way, still in java, is to download the pdf and then convert the pdf to text with PDFBox,

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, enroll in industry based Selenium online courses.

Browse Categories