【问题标题】:How to specify print location for PDF printing with Selenium如何使用 Selenium 指定 PDF 打印的打印位置
【发布时间】:2022-07-08 00:14:41
【问题描述】:

无论我做什么,文件都会一直打印到我的下载(Windows 默认)文件夹中,而不是指定的文件夹中。我做了我的研究,显然应该使用savefile.default_directory 选项而不是download.default_directory 但它无论如何都不起作用。我尝试从路径中删除尾随\\,但没有成功。这是在工作 PC 上,如果有任何区别的话,Windows 10 机器。

   import os
   os.environ["PATH"] += os.pathsep + r'C:\Program Files (x86)\Chromedriver99';

   from selenium.webdriver.chrome.options import Options
   from selenium import webdriver

   options = Options()
    options.add_experimental_option(
        "prefs",
        {
            "download.prompt_for_download": False,
            "profile.default_content_setting_values.automatic_downloads": 1,
            "download.default_directory": r"C:\Users\Lucas\Downloads\ECV\\",
            "savefile.default_directory": r"C:\Users\Lucas\Downloads\ECV\\",
            "download.directory_upgrade": True,
            "safebrowsing.enabled": True # Some answers include this, makes no difference
        },
    )
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("useAutomationExtension", False)

    # PDF printing settings
    print_settings = {
       "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": "",
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2,
        "isHeaderFooterEnabled": False,
        "isLandscapeEnabled": True
    }

    prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(print_settings)}
    options.add_experimental_option('prefs', prefs)
    options.add_argument('--kiosk-printing') # Some answers include this, makes no difference


    driver = webdriver.Chrome(options=options)
    driver.get('https://stackoverflow.com/')
    driver.execute_script('window.print();')

【问题讨论】:

    标签: python selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    您有两个问题,第一个是您设置了两次prefs,并且由于add_experimental_option() 使用字典作为选项,第二个设置覆盖了第一个设置,所有这些设置实际上都被删除了。

    self._experimental_options = {}
    
    
    def add_experimental_option(self, name, value):
        """
        Adds an experimental option which is passed to chrome.
    
        Args:
          name: The experimental option name.
          value: The option value.
        """
        self._experimental_options[name] = value
    

    第二个问题是在路径上使用原始字符串r,它的计算结果为C:\Users\Lucas\Downloads\ECV\\\\,这是无效的。使用/ 而不是\ "C:/Users/Lucas/Downloads/ECV/" 或使用\\ 而不使用r "C:\\Users\\Lucas\\Downloads\\ECV\\"

    print_settings = {
        "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": "",
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2,
        "isHeaderFooterEnabled": False,
        "isLandscapeEnabled": True
    }
    
    prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(print_settings),
             "download.prompt_for_download": False,
             "profile.default_content_setting_values.automatic_downloads": 1,
             "download.default_directory": "C:\\Users\\Lucas\\Downloads\\ECV\\",
             "savefile.default_directory": "C:\\Users\\Lucas\\Downloads\\ECV\\",
             "download.directory_upgrade": True,
             "safebrowsing.enabled": True}
    
    options = Options()
    options.add_experimental_option('prefs', prefs)
    options.add_argument('--kiosk-printing')
    
    driver = webdriver.Chrome(options=options)
    driver.get('https://stackoverflow.com/')
    driver.execute_script('window.print();')
    

    【讨论】:

    • 此解决方案仅使打印弹出窗口出现,但实际上并未将页面打印为pdf
    • @LucasSeveryn 你尝试过这两条路径吗? C:/Users/Lucas/Downloads/ECV/C:\\Users\\Lucas\\Downloads\\ECV\\ ?
    • 我尝试了r"C:/Users/Lucas/Downloads/ECV/" 和 `C:\\Users\\Lucas\\Downloads\\ECV\`,两种解决方案都在等待我在打印对话框中输入
    • @LucasSeveryn 试试options.add_argument('--kiosk-printing'),我编辑了答案。
    【解决方案2】:

    如何在无头模式下做到这一点?

    【讨论】:

    猜你喜欢
    • 2013-09-09
    • 1970-01-01
    • 2019-06-02
    • 2022-08-16
    • 2010-11-08
    • 2021-08-30
    • 2021-11-02
    • 2019-10-18
    • 2016-06-02
    相关资源
    最近更新 更多