Python Selenium 打印另存为 PDF 等待文件名输入 [英] Python Selenium Printing Save-As-PDF Waiting for Filename Input

查看:359
本文介绍了Python Selenium 打印另存为 PDF 等待文件名输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to save a website as PDF through printing dialog. My code allows me to save as pdf, but asks me to input a filename, which I don't know how to pass a filename to the pop up box. Attached is my code:

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 就很容易了.这是一个了不起的模块,可让您自动化光标.所以本质上,您需要找出弹出窗口出现的位置并使用 pyautogui 为您单击它.您只需要添加:

Oh, it is very easy if you know about pyautogui. This is an amazing module that allows you to automate your curser. So essentially, you need to figure out the place where the popup appears and use pyautogui to click it for you. All you need to add is:

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.我选择了文件名作为名称+1到1000之间的任何随机整数,以便每次保存文件时更改名称.这样,每次运行代码时都会节省时间,因为每次名称都不同.

Note: 1. I have selected the name of the file as name+any random integer between 1 to 1000 to change name every time you save the file. This way it will save every time you run the code as the names will be different every time.

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

这篇关于Python Selenium 打印另存为 PDF 等待文件名输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆