Python Selenium 打印另存为 PDF 等待文件名输入

2024-06-19

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

这些天我也有同样的问题。 在这些情况下,我在没有使用 pyautogui 的情况下解决了这个问题,因为我使用不同的电脑和显示器,并且我不想依赖于单击的位置。

我能够使用 about:config... 来解决这个问题,并通过每次必要的打印(PDF 格式)更改它们。

Ubuntu 中“PDF”打印机的名称是“打印到文件”(定义于打印打印机)并且 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'))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python Selenium 打印另存为 PDF 等待文件名输入 的相关文章

随机推荐

  • 可升级读锁的优点?

    我想知道使用可升级读锁与执行这些步骤相比有什么优势 获取读锁 检查条件以查看是否需要进行写锁定 释放读锁 采取写锁定 执行更新 释放写锁 与获取可升级读锁相比 执行上述步骤的一个明显缺点是 步骤 3 和步骤 4 之间存在一个时间窗口 其中另
  • 自动检测内部/外部开发环境

    我们使用以下函数来自动检测我们是在内部机器上还是在实时服务器上 然后为各种组件选择适当的配置 function devIsLocal res false http host SERVER HTTP HOST if http host loc
  • 根据条件延迟 Celery 任务

    有什么办法可以根据条件延迟 Celery 任务的运行吗 在它从计划变为活动之前 我想执行快速检查 看看我的机器是否可以根据提供的参数和当时机器的状态运行该任务 如果不是 它会暂停计划队列并等待 直到满足条件 我环顾了以下几点 但似乎没有解决
  • 如果浏览器在 asp .net 中关闭,请从浏览器中注销?

    我的要求有点复杂 用户正在使用 Web 浏览器访问数据库 而在访问数据库时 如果用户关闭活动页面而不是注销会话 该会话需要自动注销 有人可以指导我如何做这个吗 我在母版页中使用了jquery onbeforeunload 我收到消息离开页面
  • 有没有办法监控页面上运行的 JavaScript 函数?

    有没有办法查看页面上正在执行哪些功能 如果我在页面上加载外部脚本 是否可以动态更改函数的功能或阻止其运行 HTML5 http www w3 org TR html5 scripting 1 html establish script bl
  • Python 内置对象的 __enter__() 和 __exit__() 在哪里定义?

    我读到每次使用 with 时都会调用该对象的 enter 和 exit 方法 我知道对于用户定义的对象 您可以自己定义这些方法 但我不明白这对于 打开 等内置对象 函数甚至测试用例是如何工作的 这段代码按预期工作 我假设它使用 exit 关
  • 有没有一种从 unsigned char* 转换为 char* 的好方法?

    那些天我读了很多关于reinterpret cast lt gt 以及应该如何使用它 并在大多数情况下避免使用它 虽然我明白使用reinterpret cast lt gt 投射自 说unsigned char to char is 实施定
  • 用于在链表中查找结点的生产代码

    我在一次采访中被问到这个问题 我被要求编写代码 用于在 O 1 空间和线性时间的生产环境中在链表 其形式为 Y 形式 双臂不一定相等 中查找结点 我想出了这个解决方案 我以前在某处见过 1 Measure lengths of both l
  • 通过 SO_RCVTIMEO 套接字选项在 Ruby 中设置套接字超时

    我试图通过 SO RCVTIMEO 套接字选项在 Ruby 中设置套接字超时 但它似乎对任何最近的 nix 操作系统都没有影响 使用 Ruby 的 Timeout 模块不是一个选择 因为它需要为每个超时生成和连接线程 这可能会变得昂贵 在需
  • 移动影片剪辑时的视觉错误

    在我尝试创建一个简单的类似乒乓球的游戏时 我遇到了一个非常奇怪的 有时甚至是严重的视觉错误 当我设置球 Flash CS 5 5 库 MovieClip 以相当快的速度在屏幕上移动时 我注意到有时球的边缘会在其移动方向上被短暂切断 当它发生
  • putc 和 ungetc 和有什么区别?

    int ungetc int c FILE fp 将字符 c 推回 fp 并返回 c 或EOF对于一个错误 其中 intputc int c FILE fp 将字符 c 写入文件 fp 并返回写入的字符 或者EOF对于一个错误 这些是 K
  • jQuery Mobile - 停止缓存

    就我而言 链接加载到特殊 div 的包装器中 这些包装器是其链接的父级 我通过pageload event 当 JQuery Mobile 执行 AJAX 请求时一切正常 但是如果我点击访问的链接 jquery mobile不会发送请求 而
  • 缓存 firestore 中 get 的第一个实现

    我希望 firestore 每次都先从缓存中获取数据 根据 Firestore 文档 传递 缓存 或 服务器 选项必须启用相同的功能 下面的例子 db collection cities where capital true get cac
  • 如何模拟OutOfMemory异常

    我需要重构我的项目以使其免受OutOfMemory例外 我的项目中使用了巨大的集合 通过更改一个参数 我可以使我的程序更加准确或使用更少的内存 好吧 这就是背景 我想做的是循环运行例程 使用默认参数运行子例程 抓住OutOfMemory异常
  • 未来如何开发旧版 Firefox 插件?

    火狐浏览器正朝着网络扩展 https developer mozilla org en US Add ons WebExtensions标准承诺提高稳定性 跨浏览器兼容性和更高的安全性 与此同时 他们逐渐放弃对旧版附加组件 引导扩展 附加
  • Google Charts(AreaChart)如何检测缩放变化

    我正在画一个面积图 在覆盖层上有一些标记 我正在使用explorer选项 仅限水平 以便用户放大和缩小 问题是我找不到一种方法来通知缩放更改 以便有机会更新制造商位置 有一个图表范围变化事件 但它不是由 AreaChart 触发的 我尝试检
  • Streamjs和linqjs有什么关系

    读完SICP后 我最近发现streamjs https github com dionyziz stream js 开发商参考linqjs http linqjs codeplex com 作为具有不同语法的替代实现 但我无法建立连接 St
  • ngx-DataTable 对列进行排序无法正常工作 Angular 4

    虽然我对角度非常陌生 但我在使用 ngx DataTable 时遇到了一些困难 我使用简单的 ngx DataTable 进行简单的操作 问题出在列上 尽管我已将 attr 声明为 sortable true 但排序不起作用 这是代码 表定
  • 正确使用 JDBC 连接池 (Glassfish)

    我需要在 Java Web 服务中作为会话 bean 实现数据库连接 但我不确定我这样做是否正确 我创建了一个类 public final class SQLUtils private static DataSource m ds null
  • Python Selenium 打印另存为 PDF 等待文件名输入

    我正在尝试通过打印对话框将网站另存为 PDF 我的代码允许我另存为pdf 但要求我输入文件名 我不知道如何将文件名传递到弹出框 附上我的代码 import time from selenium import webdriver import