Using Selenium, is it possible to verify if text is present inside a PDF which is loaded by the browser?

0 votes

My application opens a PDF in the browser. I can check if the PDF was loaded with this:

verifyAttribute xpath=//embed/@src {PDF's URL}

My test would be complete if I can also check the contents of the PDF with Selenium itself. I need to check if some text is present. Is this even possibe with Selenium?

Apr 18, 2018 in Selenium by kappa3010
• 2,010 points
2,382 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Reading PDF contents is not a functionality which is natively supported by WebDriver. However, PDFBox API can be used read the content of a PDF file. The only thing to remember is, you have to shift the focus of browser window to the opened PDF. Post this, you can parse the contents of your PDF and then search for any text string.

Sample code for using PDFBox API within Selenium is below. It worked for me. You can download the JAR from here: https://pdfbox.apache.org/index.html

public void ReadPDF() throws Exception
{
//For defining the URL of PDF
URL TestURL = new URL("http://www.axmag.com/download/pdfurl-guide.pdf");

// For converting content inside PDF to text & using PDFBox API with java input stream for parsing
BufferedInputStream TestFile = new BufferedInputStream(TestURL.openStream());
PDFParser TestPDF = new PDFParser(TestFile);
TestPDF.parse();
String TestText = new PDFTextStripper().getText(TestPDF.getPDDocument());

// Use TestNG assertion for checking if your text is present in content
Assert.assertTrue(TestText.contains("Open the setting.xml, you can see it is like this")); 
}
answered Apr 18, 2018 by king_kenny
• 3,590 points

Related Questions In Selenium

0 votes
1 answer

Is it possible for a website to detect that we are using Selenium with ChromeDriver

Selenium tests for pre-defined javascript variables which ...READ MORE

answered Apr 27, 2018 in Selenium by Meci Matt
• 9,400 points
1,668 views
0 votes
2 answers

Is it possible to scroll down in a webpage using selenium webdriver programmed on python?

I using next code for facebook for ...READ MORE

answered 4 days ago in Selenium by mslavikas@gmail.com
4,189 views
0 votes
1 answer

Is it possible to do Database testing by using Selenium Webdriver?

Hey Kanika, Selenium Webdriver is limited to Testing Browser ...READ MORE

answered 5 days ago in Selenium by Anvi
• 5,680 points
14 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,030 points
56 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
421 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
43 views
0 votes
1 answer

Is it possible to manually set the attribute value of a Web Element using Selenium?

Yes, its very much possible. You just ...READ MORE

answered May 8, 2018 in Selenium by king_kenny
• 3,590 points
3,174 views
0 votes
2 answers

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.