DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk6 min

How to Test PDF Files with Selenium

Separate browser interaction from PDF validation: use Selenium for the workflow, an HTTP client for downloads, and a PDF library to inspect document bytes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Selenium to test the browser action, then validate the PDF itself with an HTTP client or a PDF library. For a download, Selenium can click a link and provide session context, but WebDriver does not expose download progress; for a PDF generated from a webpage, Selenium’s print API can return PDF data for inspection. Keep download, print-generation, and in-browser viewing tests distinct because each can fail for different reasons.

Choose the PDF workflow you need to test

Start by identifying where the PDF comes from. The right assertions depend on whether your application serves a file, displays a PDF in the browser, or creates a PDF by printing a webpage.

Workflow Use Selenium for Validate Key limitation
Download an existing PDF Find the expected link and initiate the user-facing action. Request result, saved file, and document contents. WebDriver does not report download progress; use an HTTP client for transfer checks. Selenium file-download guidance.
Generate a PDF from a webpage Call the browser print interface with the required print settings. Returned PDF bytes, saved file, extracted text, and—if required—visual layout. Print APIs differ by language binding and interface. Selenium print documentation.
Open or interact with a PDF viewer Test browser-specific display and viewer interactions. Expected viewer state, controls, or form behavior. Viewer behavior depends on browser and configuration; it is not a universal WebDriver contract. Selenium supported browsers.

Test a PDF download without relying on WebDriver progress

Selenium’s official guidance recommends using the browser to locate the link and obtain any required cookies, then retrieving the resource with an HTTP client such as curl. This separates the browser interaction from transfer and file validation.

  1. Locate the link with Selenium. Assert that the expected download control exists and obtain its destination URL. If clicking is important to the user flow, click it and verify the resulting navigation or request behavior your application exposes.
  2. Carry authentication into the HTTP request. If the PDF requires a logged-in session, collect the relevant browser cookies and supply them to the HTTP client. Do not assume an unauthenticated request tests the same path as the browser session.
  3. Request and save the file. Check the HTTP response status and write the response bytes to a controlled test location. Check the response content type as a useful signal, but do not treat it alone as proof that the file is a valid PDF.
  4. Validate the saved document. Confirm that the file exists and is non-empty, then use a PDF parser to check expected content or requirements such as PDF/A conformance.

Selenium explicitly cautions that its API does not expose download progress, so a WebDriver click by itself cannot establish that a complete file was transferred. See the official download testing guidance for the recommended separation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Freestyle 5 Books of Freestyle Self Testing Log Book Total 5 Books
  • The FreeStyle log book includes sections for: Lunch, Dinner, Bedtime, Night
  • Comments for each day of the week
  • Log Book Dimensions L=4.25" x W=3.12" x H=0.12"
  • Contains 5 book

Example: use curl after obtaining the URL and cookies

Replace the URL and cookie values with those obtained for your test session. The command writes the response to a predictable path; your test should also check the HTTP status and then inspect the saved file with a PDF library.

curl --fail --location 
  --cookie "SESSION=YOUR_SESSION_COOKIE" 
  "https://example.com/report.pdf" 
  --output /tmp/report.pdf

This is a shell example, not a Selenium API call. Integrate the HTTP request and response assertions in your test language if you need automated status checks and cleanup.

Generate a PDF from a webpage with Selenium

When printing the current page is the product behavior under test, use Selenium’s print interface and configure the options that matter to your output. The official documentation describes orientation, margins, scaling, background printing, and shrink-to-fit. The Java PrintsPage path returns base64-encoded PDF data that can be decoded and saved; Selenium also documents a BiDi BrowsingContext printing path. Consult the current print documentation for the API shape in your chosen language and interface.

  1. Navigate to the page and wait for the content that should appear in the document.
  2. Set print options to match the intended output, including orientation, margins, scaling, background output, or shrink-to-fit where relevant.
  3. Invoke the print API and obtain its PDF data. Decode base64 data when the selected binding returns it in that form.
  4. Write the decoded bytes to a test file and run document assertions against that file.

Do not test only that the print command returned successfully: the resulting bytes and the document content are the evidence that the PDF was generated as expected. The APIs and capabilities can vary across bindings, so avoid copying a code sample for another language as though it were portable.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Validate PDF content and conformance

Use a PDF-aware library for document checks rather than treating the browser’s PDF viewer as a text document. Apache PDFBox is a Java PDF library that supports Unicode text extraction, form handling, and PDF/A-1b preflight validation. Its project page lists PDFBox 3.0.8, released July 11, 2026, and 2.0.37, released July 15, 2026; those are release facts, not a universal recommendation about which version a project should use. See Apache PDFBox.

  • Text requirements: extract text and assert important labels, values, or identifiers that must be present.
  • File integrity: ensure the parser can open the file and that required pages or document structures are available.
  • PDF/A: run PDF/A-1b preflight validation only when that conformance level is a product requirement.
  • Visual requirements: text extraction does not establish layout fidelity. If page geometry, clipping, fonts, or visual appearance matter, add a rendering-based comparison appropriate to the project.

Test browser PDF viewing separately

A successful download or valid PDF does not prove that a particular browser presents it as expected. Firefox uses its built-in viewer when PDFs are configured to open in Firefox, which Mozilla says is the default setting; an incorrectly set MIME type is a documented exception. See Mozilla’s Firefox PDF viewer guidance.

Keep viewer-specific assertions scoped to the browser and configuration you support. Selenium documents browser-specific capabilities, so do not assume viewer selectors or driver preferences are portable across browsers. If the application contract is the PDF response itself, assert the response and document rather than relying on a browser viewer’s internal UI.

Common failures and how to diagnose them

  • The test clicks Download but cannot tell whether transfer finished. This is expected: WebDriver does not expose download progress. Retrieve the file through an HTTP client and assert its response and contents.
  • The HTTP request returns an authorization error. The request may not include the browser session’s required cookies or other authentication. Transfer the necessary session state into the HTTP client request, and verify that it targets the link found by Selenium.
  • A file exists but PDF parsing fails. A saved response is not necessarily a valid PDF; it could be an error page or incomplete response. Check the HTTP status and inspect the bytes with a PDF parser before treating it as a document.
  • Text assertions pass but the page looks wrong. Text extraction cannot verify visual fidelity. Add a rendered-page check for layout-sensitive requirements.
  • The PDF opens in a different place or viewer than expected. Browser settings and MIME type influence presentation. Scope the test to the browser configuration and check the served MIME type where relevant.
  • A print call works in one binding but not another. Selenium has language- and interface-specific print APIs. Use the current documentation for the binding and interface in your project rather than assuming a single cross-language call.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your test needs a screenshot or PDF capture of a webpage rather than validation of your application’s download or print workflow, ScreenshotNeo offers a one-request capture API. For example, this cURL request saves a WebP screenshot of the target page:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Accent on Composers: The Music and Lives of 22 Great Composers, with Listening CD, Review/Tests, and Supplemental Materials, Comb Bound Book & Online PDF/Audio
  • Format: Comb Bound Book & Enhanced CD
  • Version: CD Kit (Book & Enhanced CD) (Includes Reproducible Student Pages)
  • Category: General Music and Classroom Publications
  • Contributors: By Jay Althouse and Judy O'Reilly
  • Pub Date: 7/2001
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo documentation for request options. It removes cookie/consent banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are not billed. An MCP server provides screenshot tools for AI agents, and the free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. This captures a webpage; it does not replace testing the authenticated download or print behavior your Selenium test is meant to cover.

Sign up for 1,000 free screenshots a month, with no card required.

Frequently Asked Questions

Can Selenium validate the text inside a downloaded PDF?

Selenium handles browser interaction; use a PDF library such as Apache PDFBox to extract and assert document text.

Does Selenium’s print API work the same way in every language?

No. Use the current Selenium print documentation for your language binding and chosen interface.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Should I automate the built-in PDF viewer for every browser?

Only for browser-specific viewer behavior you need to support. Viewer behavior and capabilities vary by browser and configuration.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.