Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python自动从windows文件对话框打开文件_Python_Windows_Dialog_Automated Tests_Ui Automation - Fatal编程技术网

使用python自动从windows文件对话框打开文件

使用python自动从windows文件对话框打开文件,python,windows,dialog,automated-tests,ui-automation,Python,Windows,Dialog,Automated Tests,Ui Automation,我做自动测试,并得到一个文件对话框。我想使用python或selenium从windows“打开文件”对话框中选择一个文件 注:该对话框由其他程序提供。我不想用Tkinter创建它 窗口看起来像: 如何做到这一点?您可以使用ctypes库 考虑以下代码: import ctypes EnumWindows = ctypes.windll.user32.EnumWindows EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.

我做自动测试,并得到一个文件对话框。我想使用python或selenium从windows“打开文件”对话框中选择一个文件

注:该对话框由其他程序提供。我不想用Tkinter创建它

窗口看起来像:


如何做到这一点?

您可以使用ctypes库

考虑以下代码:

import ctypes

EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
SendMessage = ctypes.windll.user32.SendMessageW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible

def foreach_window(hwnd, lParam):
    if IsWindowVisible(hwnd):
        length = GetWindowTextLength(hwnd)
        buff = ctypes.create_unicode_buffer(length + 1)
        GetWindowText(hwnd, buff, length + 1)

        if(buff.value == "Choose File to Upload"): #This is the window label
            SendMessage(hwnd, 0x0100, 0x09, 0x00000001 )
    return True

EnumWindows(EnumWindowsProc(foreach_window), 0)
在每个打开的窗口上循环,然后向选择的窗口发送一个关键点笔划

该函数获取4个参数:窗口hendler(
hwnd
)、要发送的物理键-(0x0100)、选项卡的of(
0x09
)以及第4个参数中的
重复计数、扫描代码、扩展键标志、上下文代码、上一个键状态标志和转换状态标志

您还可以发送键向上,键向下,字符,返回等。。。 使用文档获取帮助

我将此作为参考:

祝你好运

考虑使用该软件包。它有一个非常自然的语法来自动化任何GUI程序

代码示例,在记事本中打开文件。请注意,语法依赖于区域设置(它使用GUI程序中的可见窗口标题/控件标签):


@Alvin的可能副本请参见注释。Selenium不会帮助您。您是否考虑过,。您是想随机选择一个文件,遍历所有文件,还是选择一个特定的文件?我会在您的答案中添加一些代码。您还可以描述如何单击第一个文件吗?如何获取元素名称?谢谢
from pywinauto import application
app = application.Application().start_('notepad.exe')
app.Notepad.MenuSelect('File->Open')
# app.[window title].[control name]...
app.Open.Edit.SetText('filename.txt')
app.Open.Open.Click()