首页 > 解决方案 > Python Selenium 打印另存为 PDF 等待文件名输入

问题描述

我正在尝试通过打印对话框将网站另存为 PDF。我的代码允许我保存为 pdf,但要求我输入一个文件名,我不知道如何将文件名传递给弹出框。附上我的代码:

import time
from selenium import webdriver
import os

class printing_browser(object):
    def __init__(self):
        self.profile = webdriver.FirefoxProfile()
        self.profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False)
        self.profile.set_preference("pdfjs.disabled", True)
        self.profile.set_preference("print.always_print_silent", True)
        self.profile.set_preference("print.show_print_progress", False)
        self.profile.set_preference("browser.download.show_plugins_in_list",False)
        foxdriver = r'C:\Users\AShen\Documents\Workspace\geckodriver.exe'
        self.driver = webdriver.Firefox(executable_path=foxdriver,firefox_profile = self.profile)
        time.sleep(5)

    def get_page_and_print(self, page):
        self.driver.get(page)
        time.sleep(5)
        self.driver.execute_script("window.print();")

if __name__ == "__main__":
    browser_that_prints = printing_browser()
    browser_that_prints.get_page_and_print('http://www.google.com/')

标签: pythonseleniumpdfprintingselenium-chromedriver

解决方案


这些天我有同样的问题。在这种情况下,我没有使用 pyautogui 就解决了这个问题,因为我使用不同的 PC 和显示器,我不想依赖于点击的位置。

我能够使用 about:config... 用每个必要的打印(PDF 格式)更改它们来解决它。

我的打印机在 Ubuntu 中的“PDF”名称是“打印到文件”(在print_printer中定义)并且 about:config 的设置需要是这台打印机......例如:print.printer_Print_to_File.print_to_file: true

import os
import time
from selenium import webdriver

class printing_browser(object):
    def __init__(self):
        self.profile = webdriver.FirefoxProfile()
        self.profile.set_preference('services.sync.prefs.sync.browser.download.manager.showWhenStarting', False)
        self.profile.set_preference('pdfjs.disabled', True)
        self.profile.set_preference('print.always_print_silent', True)
        self.profile.set_preference('print.show_print_progress', False)
        self.profile.set_preference('browser.download.show_plugins_in_list', False)
        
        self.profile.set_preference('browser.download.folderList', 2)
        self.profile.set_preference('browser.download.dir', '')
        self.profile.set_preference('browser.download.manager.showWhenStarting', False)
        self.profile.set_preference('browser.aboutConfig.showWarning', False)
        
        self.profile.set_preference('print.print_headerright', '')
        self.profile.set_preference('print.print_headercenter', '')
        self.profile.set_preference('print.print_headerleft', '')
        self.profile.set_preference('print.print_footerright', '')
        self.profile.set_preference('print.print_footercenter', '')
        self.profile.set_preference('print.print_footerleft', '')
        self.profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/octet-stream;application/vnd.ms-excel;text/html')
        
        foxdriver = r'C:\Users\AShen\Documents\Workspace\geckodriver.exe'
        self.driver = webdriver.Firefox(
            executable_path=foxdriver,
            firefox_profile=self.profile
        )
        time.sleep(1)

    def get_page_and_print(self, page, filepath):
        # Get about:config
        self.driver.get('about:config')
        time.sleep(1)

        # Define Configurations
        script = """
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
        prefs.setBoolPref('print.always_print_silent', true);
        prefs.setCharPref('print_printer', 'Print to File');
        prefs.setBoolPref('print.printer_Print_to_File.print_to_file', true);
        prefs.setCharPref('print.printer_Print_to_File.print_to_filename', '{}');
        prefs.setBoolPref('print.printer_Print_to_File.show_print_progress', true);
        """.format(filepath)

        # Set Configurations
        self.driver.execute_script(script)
        time.sleep(1)

        # Get site to print in pdf
        self.driver.get(page)
        time.sleep(2)
        self.driver.execute_script("window.print();")

        

browser_that_prints = printing_browser()
browser_that_prints.get_page_and_print('http://www.google.com', os.path.join(os.getcwd(), 'mywebpage.pdf'))

推荐阅读