【问题标题】:Python Selenium Printing Save-As-PDF Waiting for Filename InputPython Selenium 打印另存为 PDF 等待文件名输入
【发布时间】:2019-11-27 22:21:23
【问题描述】:

我正在尝试通过打印对话框将网站另存为 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/')

【问题讨论】:

  • 抱歉,您能解释一下the pop up box 的意思吗?也许进一步解释一下流程。
  • 由于 selenium 使用页面标题作为 PDF 文件名,因此只需在打印前将页面标题更改为您要为 PDF 指定的名称。 driver.execute_script('document.title="{}";'.format(YOUR_PDF_NAME)); driver.execute_script('window.print();')

标签: python selenium pdf printing selenium-chromedriver


【解决方案1】:

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

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

我的打印机“in PDF”在 Ubuntu 中的名称是“打印到文件”(在 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'))

【讨论】:

    【解决方案2】:

    哦,如果你知道pyautogui,那就很容易了。 这是一个令人惊叹的模块,可让您自动化您的光标。 所以本质上,您需要找出弹出窗口出现的位置并使用pyautogui 为您单击它。 您只需要添加:

    time.sleep(3)
    
    i=random.randint(0,1000)
    file_name=('name_pdf '+str(i))
    print (file_name)
    
    
    pyautogui.typewrite(file_name)
    pyautogui.click(512,449)
    

    整个代码结构如下所示:

    import time
    import pyautogui
    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\Pranjal Pathak\Desktop\Titanic Kaggle\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.python.org/')
    
    time.sleep(3)
    
    i=random.randint(0,1000)
    file_name=('name_pdf '+str(i))
    print (file_name)
    
    
    pyautogui.typewrite(file_name)
    pyautogui.click(512,449)
    

    注意:1.我选择了文件的名称为name+1到1000之间的任意整数,以便每次保存文件时更改名称。这样每次运行代码时都会保存,因为每次名称都会不同。

    1. 如果这键入了名称但不保存文件,您可能需要更改光标的坐标。如果发生这种情况,请告诉我。

    【讨论】:

    • 很高兴为您提供帮助,爱丽丝。 :)
    猜你喜欢
    • 2019-11-30
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    相关资源
    最近更新 更多