问题描述
我进行自动化测试并获得一个文件对话框.我想用 python 或 selenium 从 windows 打开文件对话框中选择一个文件.
注意:该对话框由其他程序提供.我不想用 Tkinter 创建它.
窗口看起来像:
.
如何做到这一点?
解决方案
考虑使用
代码示例,在记事本中打开文件.请注意,语法取决于区域设置(它使用 GUI 程序中可见的窗口标题/控件标签):
来自 pywinauto 导入应用程序app = application.Application().start_('notepad.exe')app.Notepad.MenuSelect('文件->打开')# app.[窗口标题].[控件名称]...app.Open.Edit.SetText('filename.txt')app.Open.Open.Click()
I do automated testing and get a file dialog. I want to choose a file from the windows open file dialog with python or selenium.
NOTE: The dialog is given by an other program. I don't want to create it with Tkinter.
The Window looks like:
.
How to do this?
解决方案
Consider using the pywinauto package. It has a very natural syntax to automate any GUI programs.
Code example, opening a file in notepad. Note that the syntax is locale dependent (it uses the visible window titles / control labels in your GUI program):
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()