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,090 points
10,755 views

1 answer to this question.

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,710 points

Related Questions In Selenium

0 votes
1 answer
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,460 points
4,322 views
+1 vote
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 May 16, 2019 in Selenium by mslavikas@gmail.com
18,655 views
0 votes
1 answer
0 votes
2 answers

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,520 points
3,770 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,752 views
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,890 points
2,733 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,096 views
0 votes
2 answers

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

WebDriver driver; JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.getElementById('id123').setAttribute('attr', ...READ MORE

answered Sep 3 in Selenium by Sri
• 2,970 points
15,631 views
+1 vote
2 answers