Print Style Sheet and Export a Webpage to PDF in Selenium WebDriver

Print style sheets format a web page for printing.

Zhimin Zhan

--

A repost of my daughter’s article with permission. I added a few notes. This is also included in my “How to in Selenium WebDriver” series.

Exporting a web page to PDF in automated tests as a test artifact can be useful for manual inspection later. For example, a suite of automated tests might generate multiple different customer invoices. After execution, manual testers can open and verify the printed invoices (PDF) look as expected.

Selenium v4 made exporting to PDF easy with driver.save_print_page.

Sample page: login in WhenWise as the test business account and view an invoice. The page looks like this:

If using `driver.save_screenshot`, you will get this PNG image

Below is the current PDF-printed version of a WhenWise invoice (admin view, strictly no need to be printer-friendly, I will make it so):

Current WhenWise invoice page (admin view) exported to PDF

--

--

Zhimin Zhan

Test automation & CT coach, author, speaker and award-winning software developer. Help teams succeed with Agile/DevOps by implementing real Continuous Testing.