Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
2.9k views
by Contributing Tester (24 points)
after clicking on some button a pdf popup is displayed .i want to validate the text in thet pdf. we have code in java already but i need it in python.Please help me.and also if we download the file that is pdf for an exmaple,how to validate the text in that file using selenium python code.

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)

Validating the test from PDF is different then selenium library. You will need to use PDF libraries for python. One of the libraries I have used so far is PyPDF2.

Sample code:

import PyPDF2
pdf_file = open('sample.pdf', 'rb')
read_pdf = PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
page = read_pdf.getPage(0)
page_content = page.extractText()
print page_content.encode('utf-8')

You can even try using textract library, if your PDF encoding is supported, extracting text will be very easy.

Sample code:

import textract
text = textract.process("path/to/file.extension")

Once your texts are extracted and stored into variable, you just have to assert it.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...