在寻找可能的解决方案时,我遇到了SO和其他方面的几个解决方案.

他们中的一些人正在使用AutoIT,或者编辑浏览器配置文件以使其直接存储文件而不提示.

我发现所有这个解决方案都太具体了,就像你可以通过编辑浏览器配置文件来解决“另存为”对话框的问题,但是如果以后你需要处理其他窗口那么你就会陷入困境.

对于使用AutoIT是过度杀伤,这直接与我选择Python来执行此任务的事实相冲突. (我的意思是Python本身就是如此强大,取决于其他一些工具严格禁止任何Python教徒)

所以经过长时间搜索这个问题的可能的通用解决方案,它不仅为任何想要在使用selenium自动化Web应用程序的过程中处理任何Native OS对话框(如“另存为”,“文件上载”等)提供服务. Web驱动程序,也适用于任何只想使用Python API与特定窗口进行交互的人.

该解决方案使用了Python的Win32gui,SendKeys模块.

我将首先解释一个通用方法来获取所需的任何窗口,然后添加一个小代码,这也可以使用Selenium Webdriver自动化Web应用程序时使用它.

通用解决方案::

import win32gui
import re
import SendKeys
class WindowFinder:
"""Class to find and make focus on a particular Native OS dialog/Window """
def __init__ (self):
self._handle = None
def find_window(self, class_name, window_name = None):
"""Pass a window class name & window name directly if known to get the window """
self._handle = win32gui.FindWindow(class_name, window_name)
def _window_enum_callback(self, hwnd, wildcard):
'''Call back func which checks each open window and matches the name of window using reg ex'''
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) != None:
self._handle = hwnd
def find_window_wildcard(self, wildcard):
""" This function takes a string as input and calls EnumWindows to enumerate through all open windows """
self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)
def set_foreground(self):
"""Get the focus on the desired open window"""
win32gui.SetForegroundWindow(self._handle)
win = WindowFinder()
win.find_window_wildcard(".*Save As.*")
win.set_foreground()
path = "D:\\File.txt" #Path of the file you want to Save
ent = "{ENTER}" #Enter key stroke.
SendKeys.SendKeys(path) #Use SendKeys to send path string to Save As dialog
SendKeys.SendKeys(ent) #Use SendKeys to send ENTER key stroke to Save As dialog

要使用此代码,您需要提供一个字符串,该字符串是您要获取的窗口的名称,在本例中为“另存为”.所以类似地,您可以提供任何名称并使该窗口集中.

获得所需窗口的焦点后,您可以使用SendKeys模块将键击发送到窗口,在这种情况下,包括发送要保存文件的文件路径和ENTER.

特定于Selenium Webdriver ::

上面指定的代码段可用于处理本机操作系统对话框,这些对话框在自动化期间通过Web应用程序使用Selenium Webdriver添加一点代码来触发.

您在使用此代码时遇到的问题是,一旦您的自动化代码点击任何触发本机操作系统对话框窗口的Web元素,控件将停留在该点,等待本机操作系统对话框窗口上的任何操作.所以基本上你会陷入困境.

解决方法是使用Python线程模块生成一个新线程,并使用它来单击Web元素以触发本机操作系统对话框,并且您的父线程将正常运行以使用上面显示的代码查找窗口.

#Assume that at this point you are on the page where you need to click on a Web Element to trigger native OS window/dialog box
def _action_on_trigger_element(_element):
_element.click()
trigger_element = driver.find_element_by_id('ID of the Web Element which triggers the window')
th = threading.Thread(target = _action_on_trigger_element, args = [trigger_element]) #Thread is created here to call private func to click on Save button
th.start() #Thread starts execution here
time.sleep(1) #Simple Thread Synchronization handle this case.
#Call WindowFinder Class
win = WindowFinder()
win.find_window_wildcard(".*Save As.*")
win.set_foreground()
path = "D:\\File.txt" #Path of the file you want to Save
ent = "{ENTER}" #Enter key stroke.
SendKeys.SendKeys(path) #Use SendKeys to send path string to Save As dialog
SendKeys.SendKeys(ent) #Use SendKeys to send ENTER key stroke to Save As dialog
#At this point the native OS window is interacted with and closed after passing a key stroke ENTER.
# Go forward with what ever your Automation code is doing after this point

注意::

在自动化Web应用程序中使用上述代码时,请检查要查找的窗口的名称,并将其传递给find_window_wildcard(). Windows的名称取决于浏览器.例如.单击元素以上载文件时触发的窗口在Firefox中称为“文件上载”,在Chrome中称为“打开”.

使用Python2.7

我希望这可以帮助任何正在寻找类似解决方案的人,无论是以任何通用形式使用它还是自动化Web应用程序.

编辑:

如果您尝试通过命令行参数运行代码,则尝试使用该线程使用Win32gui查找窗口并使用原始程序线程单击该元素(使用该线程单击此处).原因是urllib库在使用线程创建新连接时会抛出错误.)