【问题标题】:How to control a "Save as" window that opens on a webpage, using Python?如何使用 Python 控制在网页上打开的“另存为”窗口?
【发布时间】:2020-06-13 22:53:00
【问题描述】:

所以我使用 Selenium 和 Python 来浏览网站,然后单击打开“另存为”窗口的按钮。我怎样才能控制那个窗口?我需要先发送一个保存文件的路径并修改文件名,然后点击“保存”。

我认为我无法使用 Selenium 执行此操作,因为打开它的“另存为”窗口是一个系统对话框窗口。

我应该使用什么工具或 Python 脚本来完成我需要做的事情? 如何使用 Python 控制系统对话窗口?

【问题讨论】:

    标签: python selenium selenium-webdriver automation webdriver


    【解决方案1】:

    您可以使用 pyautogui 等 GUI 自动化包。代码cmets中的解释。

    import pyautogui
    import time
    
    # To simulate a Save As dialog. You can remove this since you'll be saving/downloading a file from a link
    pyautogui.hotkey('ctrl', 's')
    # Wait for the Save As dialog to load. Might need to increase the wait time on slower machines
    time.sleep(1)
    # File path + name
    FILE_NAME = 'C:\\path\\to\\file\\file.ext'
    # Type the file path and name is Save AS dialog
    pyautogui.typewrite(FILE_NAME)
    #Hit Enter to save
    pyautogui.hotkey('enter')
    

    【讨论】:

    • 非常感谢,效果很好!!!!!!!另外,你能告诉我是否有办法将“file.ext”变成我可以改变的参数吗?语法如何?
    • 您可以将变量 FILE_NAME = 'C:\\path\\to\\file\\file.ext' 更改为任何有效的路径和文件名。
    • 我可以更改保存类型:网页,单个文件(*.mhtml)吗?
    • 什么确保 pyautogui 作用于 Selenium 窗口而不是任何其他打开的窗口?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 2013-06-30
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多