【发布时间】: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