打印页面

打印网页是一项常见任务,无论是用于共享信息还是维护存档。Selenium 通过其 PrintOptions、PrintsPage 和 browsingContext 类简化了此过程,这些类为自动化打印网页提供了灵活且直观的接口。这些类使您能够配置打印首选项,例如页面布局、边距和缩放,确保输出满足您的特定要求。

配置

方向

使用 getOrientation()setOrientation() 方法,您可以获取/设置页面方向 — PORTRAIT(纵向)或 LANDSCAPE(横向)。

    public void TestOrientation() 
    {
        driver.get("https://seleniumcn.cn/");
        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://seleniumcn.cn");
            PrintOptions printOptions  = new PrintOptions();
            printOptions.Orientation = PrintOrientation.Landscape;
            PrintOrientation currentOrientation = printOptions.Orientation;
        }
    driver.get("https://seleniumcn.cn/")
    print_options = PrintOptions()
    print_options.orientation = "landscape" ## landscape or portrait

范围

使用 getPageRanges()setPageRanges() 方法,您可以获取/设置要打印的页面范围 — 例如“2-4”。

    public void TestRange() 
    {
        driver.get("https://seleniumcn.cn/");
        PrintOptions printOptions = new PrintOptions();
        printOptions.setPageRanges("1-2");
        String[] current_range = printOptions.getPageRanges();
    }
        public void TestRange()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://seleniumcn.cn");
            PrintOptions printOptions  = new PrintOptions();
            printOptions.AddPageRangeToPrint("1-3"); // add range of pages
            printOptions.AddPageToPrint(5); // add individual page
        }   
    driver.get("https://seleniumcn.cn/")
    print_options = PrintOptions()
    print_options.page_ranges = ["1, 2, 3"] ## ["1", "2", "3"] or ["1-3"]

尺寸

使用 getPaperSize()setPaperSize() 方法,您可以获取/设置要打印的纸张尺寸 — 例如“A0”、“A6”、“Legal”、“Tabloid”等。

    public void TestSize() 
    {
        driver.get("https://seleniumcn.cn/");
        PrintOptions printOptions = new PrintOptions();
        printOptions.setScale(.50);
        double current_scale = printOptions.getScale();
    }
        public void TestSize()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://seleniumcn.cn/");
            PrintOptions printOptions = new PrintOptions();
            PrintOptions.PageSize currentDimensions = printOptions.PageDimensions;
        }
    driver.get("https://seleniumcn.cn/")
    print_options = PrintOptions()
    print_options.scale = 0.5 ## 0.1 to 2.0``

边距

使用 getPageMargin()setPageMargin() 方法,您可以设置要打印页面的边距大小 — 即上、下、左和右边距。

    {
        driver.get("https://seleniumcn.cn/");
        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://seleniumcn.cn/");
            PrintOptions printOptions = new PrintOptions();
            PrintOptions.Margins currentMargins = printOptions.PageMargins;
        }
    driver.get("https://seleniumcn.cn/")
    print_options = PrintOptions()
    print_options.margin_top = 10
    print_options.margin_bottom = 10
    print_options.margin_left = 10
    print_options.margin_right = 10

比例

使用 getScale()setScale() 方法,您可以获取/设置要打印页面的比例 — 例如,1.0 是 100% 或默认值,0.25 是 25% 等。

    public void TestScale() 
    {
        driver.get("https://seleniumcn.cn/");
        PrintOptions printOptions = new PrintOptions();
        printOptions.setScale(.50);
        double current_scale = printOptions.getScale();
    }
        public void TestScale()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://seleniumcn.cn/");
            PrintOptions printOptions = new PrintOptions();
            printOptions.ScaleFactor = 0.5;
            double currentScale = printOptions.ScaleFactor;
        }
    driver.get("https://seleniumcn.cn/")
    print_options = PrintOptions()
    print_options.scale = 0.5 ## 0.1 to 2.0
    current_scale = print_options.scale

背景

使用 getBackground()setBackground() 方法,您可以获取/设置是否显示背景颜色和图像 — 布尔值 truefalse

    public void TestBackground() 
    {
        driver.get("https://seleniumcn.cn/");
        PrintOptions printOptions = new PrintOptions();
        printOptions.setBackground(true);
        boolean current_background = printOptions.getBackground();
    }
        public void TestBackgrounds()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://seleniumcn.cn/");
            PrintOptions printOptions = new PrintOptions();
            printOptions.OutputBackgroundImages = true;
            bool currentBackgrounds = printOptions.OutputBackgroundImages;
        }
    driver.get("https://seleniumcn.cn/")
    print_options = PrintOptions()
    print_options.background = True ## True or False

自动缩放

使用 getShrinkToFit()setShrinkToFit() 方法,您可以获取/设置页面是否会缩小以适应页面上的内容 — 布尔值 truefalse

    public void TestShrinkToFit() 
    {
        driver.get("https://seleniumcn.cn/");
        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://seleniumcn.cn/");
            PrintOptions printOptions = new PrintOptions();
            printOptions.ShrinkToFit = true;
            bool currentShrinkToFit = printOptions.ShrinkToFit;
        }
    driver.get("https://seleniumcn.cn/")
    print_options = PrintOptions()
    print_options.shrink_to_fit = True ## True or False

打印

配置好 PrintOptions 后,您就可以打印页面了。为此,您可以调用 print 函数,该函数会生成网页的 PDF 表示形式。生成的 PDF 可以保存到本地存储以供进一步使用或分发。 使用 PrintsPage(),print 命令将以 base64 编码格式返回 PDF 数据,该数据可以解码并写入您所需位置的文件中,而使用 BrowsingContext() 将返回一个字符串。

目前可能存在多种实现,具体取决于您选择的语言。例如,使用 Java,您可以选择使用 BrowingContext()PrintsPage() 进行打印。两者都将 PrintOptions() 对象作为参数。

注意:BrowsingContext() 是 Selenium 的 BiDi 实现的一部分。要启用 BiDi,请参阅 启用 Bidi

PrintsPage()

    public void PrintWithPrintsPageTest() 
    {
        driver.get("https://seleniumcn.cn/");
        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://seleniumcn.cn/selenium/web/formPage.html");
        PrintOptions printOptions = new PrintOptions();
        String printPage = browsingContext.print(printOptions);
        Assertions.assertTrue(printPage.length() > 0);
    }
        public void PrintWithPrintsPageTest() 
        {
            WebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://seleniumcn.cn/");
            PrintOptions printOptions = new PrintOptions();
            PrintDocument printedPage = driver.Print(printOptions);
            Assert.IsTrue(printedPage.AsBase64EncodedString.StartsWith("JVBER"));
        }

print_page()

    driver.get("https://seleniumcn.cn/")
    print_options = PrintOptions()
    pdf = driver.print_page(print_options)