Print Page
Printing a webpage is a common task, whether for sharing information or maintaining archives. Selenium simplifies this process through its PrintOptions, PrintsPage, and browsingContext classes, which provide a flexible and intuitive interface for automating the printing of web pages. These classes enable you to configure printing preferences, such as page layout, margins, and scaling, ensuring that the output meets your specific requirements.
Configuring
Orientation
Using the getOrientation()
and setOrientation()
methods, you can get/set the page orientation — either PORTRAIT
or LANDSCAPE
.
public void TestOrientation()
{
driver.get("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
printOptions.setOrientation(PrintOptions.Orientation.LANDSCAPE);
PrintOptions.Orientation current_orientation = printOptions.getOrientation();
}
public void TestOrientation()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://selenium.dev");
PrintOptions printOptions = new PrintOptions();
printOptions.Orientation = PrintOrientation.Landscape;
PrintOrientation currentOrientation = printOptions.Orientation;
}
def test_orientation(driver):
driver.get("https://www.selenium.dev/")
print_options = PrintOptions()
print_options.orientation = "landscape" ## landscape or portrait
assert print_options.orientation == "landscape"
Range
Using the getPageRanges()
and setPageRanges()
methods, you can get/set the range of pages to print — e.g. “2-4”.
public void TestRange()
{
driver.get("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
printOptions.setPageRanges("1-2");
String[] current_range = printOptions.getPageRanges();
}
public void TestRange()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://selenium.dev");
PrintOptions printOptions = new PrintOptions();
printOptions.AddPageRangeToPrint("1-3"); // add range of pages
printOptions.AddPageToPrint(5); // add individual page
}
def test_range(driver):
driver.get("https://www.selenium.dev/")
print_options = PrintOptions()
print_options.page_ranges = ["1, 2, 3"] ## ["1", "2", "3"] or ["1-3"]
assert print_options.page_ranges == ["1, 2, 3"]
Size
Using the getPaperSize()
and setPaperSize()
methods, you can get/set the paper size to print — e.g. “A0”, “A6”, “Legal”, “Tabloid”, etc.
public void TestSize()
{
driver.get("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
printOptions.setScale(.50);
double current_scale = printOptions.getScale();
}
public void TestSize()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
PrintOptions.PageSize currentDimensions = printOptions.PageDimensions;
}
def test_size(driver):
driver.get("https://www.selenium.dev/")
print_options = PrintOptions()
print_options.scale = 0.5 ## 0.1 to 2.0``
assert print_options.scale == 0.5
Margins
Using the getPageMargin()
and setPageMargin()
methods, you can set the margin sizes of the page you wish to print — i.e. top, bottom, left, and right margins.
{
driver.get("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
PageMargin margins = new PageMargin(1.0,1.0,1.0,1.0);
printOptions.setPageMargin(margins);
double topMargin = margins.getTop();
double bottomMargin = margins.getBottom();
double leftMargin = margins.getLeft();
double rightMargin = margins.getRight();
}
public void TestMargins()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
PrintOptions.Margins currentMargins = printOptions.PageMargins;
}
def test_margin(driver):
driver.get("https://www.selenium.dev/")
print_options = PrintOptions()
print_options.margin_top = 10
print_options.margin_bottom = 10
print_options.margin_left = 10
print_options.margin_right = 10
assert print_options.margin_top == 10
assert print_options.margin_bottom == 10
assert print_options.margin_left == 10
assert print_options.margin_right == 10
Scale
Using getScale()
and setScale()
methods, you can get/set the scale of the page you wish to print — e.g. 1.0 is 100% or default, 0.25 is 25%, etc.
public void TestScale()
{
driver.get("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
printOptions.setScale(.50);
double current_scale = printOptions.getScale();
}
public void TestScale()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
printOptions.ScaleFactor = 0.5;
double currentScale = printOptions.ScaleFactor;
}
def test_scale(driver):
driver.get("https://www.selenium.dev/")
print_options = PrintOptions()
print_options.scale = 0.5 ## 0.1 to 2.0
current_scale = print_options.scale
assert current_scale == 0.5
Background
Using getBackground()
and setBackground()
methods, you can get/set whether background colors and images appear — boolean true
or false
.
public void TestBackground()
{
driver.get("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
printOptions.setBackground(true);
boolean current_background = printOptions.getBackground();
}
public void TestBackgrounds()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
printOptions.OutputBackgroundImages = true;
bool currentBackgrounds = printOptions.OutputBackgroundImages;
}
def test_background(driver):
driver.get("https://www.selenium.dev/")
print_options = PrintOptions()
print_options.background = True ## True or False
assert print_options.background is True
ShrinkToFit
Using getShrinkToFit()
and setShrinkToFit()
methods, you can get/set whether the page will shrink-to-fit content on the page — boolean true
or false
.
public void TestShrinkToFit()
{
driver.get("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
printOptions.setShrinkToFit(true);
boolean current_shrink_to_fit = printOptions.getShrinkToFit();
}
public void TestShrinkToFit()
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.selenium.dev/");
PrintOptions printOptions = new PrintOptions();
printOptions.ShrinkToFit = true;
bool currentShrinkToFit = printOptions.ShrinkToFit;
}
def test_shrink_to_fit(driver):
driver.get("https://www.selenium.dev/")
print_options = PrintOptions()
print_options.shrink_to_fit = True ## True or False
assert print_options.shrink_to_fit is True
Printing
Once you’ve configured your PrintOptions, you’re ready to print the page. To do this,
you can invoke the print function, which generates a PDF representation of the web page.
The resulting PDF can be saved to your local storage for further use or distribution.
Using PrintsPage()
, the print command will return the PDF data in base64-encoded format, which can be decoded
and written to a file in your desired location, and using BrowsingContext()
will return a String.
There may currently be multiple implementations depending on your language of choice. For example, with Java you
have the ability to print using either BrowingContext()
or PrintsPage()
. Both take PrintOptions()
objects as a
parameter.
Note: BrowsingContext()
is part of Selenium’s BiDi implementation. To enable BiDi see Enabling Bidi
PrintsPage()
public void PrintWithPrintsPageTest()
{
driver.get("https://www.selenium.dev/");
PrintsPage printer = (PrintsPage) driver;
PrintOptions printOptions = new PrintOptions();
Pdf printedPage = printer.print(printOptions);
Assertions.assertNotNull(printedPage);
}
BrowsingContext()
public void PrintWithBrowsingContextTest()
{
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
driver.get("https://www.selenium.dev/selenium/web/formPage.html");
PrintOptions printOptions = new PrintOptions();
String printPage = browsingContext.print(printOptions);
Assertions.assertTrue(printPage.length() > 0);
}
print_page()
def test_prints_page(driver):
driver.get("https://www.selenium.dev/")
print_options = PrintOptions()
pdf = driver.print_page(print_options)
assert len(pdf) > 0