Qt:接管系统打开文件对话框

问题描述 投票:2回答:3

我想使用PyQt4的QWebView自动将文件上传到网站,但是有些地方我还不知道。要上传文件,网站上有一个按钮,它会打开一个对话框,您应从中选择本地文件。所以,这些是我的问题:)单击按钮后,是否可以控制该对话框?有没有更好的方法来实现这一目标?

edit

该网站为https://maps.google.com/,我正在通过我的位置>创建地图>导入上传一个.kml文件。

python qt pyqt4 qwebview qfiledialog
3个回答
4
投票

[您可能正在寻找的是QWebPage::chooseFile()(我想这还取决于网站如何处理)。重新实现它,看看是否足够。做任何您想做的事情并返回选择的文件路径。

编辑:现在您提供了我测试过的链接,并且似乎可以正常工作。


1
投票

好,首先让我开始了解背景信息和参考。

我将使用的模块是pywin32下载here,特别是win32gui,API参考here

现在在操作对话框之前,您必须“导航”到窗口句柄,以下使用win32.FindWindow API参考here,看起来像这样,在这种情况下,两个输入是lpclassName [ C0](代表对话框)参考#32770here,在这种情况下为lpWindowName

File Upload

找到文件句柄的代码:

HWND WINAPI FindWindow(
  _In_opt_  LPCTSTR lpClassName,
  _In_opt_  LPCTSTR lpWindowName
); 

并且它存储了句柄,在我的情况下是import win32gui control = win32gui.FindWindow("#32770", "File Upload")

下一步是在对话框中找到GUI对象的句柄,我将显示721470按钮的示例。要找到该句柄,我将在此处使用Cancel API参考,

FindWindowEx

import win32con import win32api ButtonHandle = win32gui.FindWindowEx(control, 0, "Button", "Cancel"); win32api.SendMessage(ButtonHandle, win32con.BM_CLICK, 0, 0) 的参考hereBM_CLICKhere

最终代码:

SendMessage

另一种方法是使用import win32gui import win32api import win32con window = win32gui.GetForegroundWindow() title = win32gui.GetWindowText(window) control = win32gui.FindWindow("#32770", "File Upload") ButtonHandle = win32gui.FindWindowEx(control, 0, "Button", "Cancel") win32api.SendMessage(ButtonHandle, win32con.BM_CLICK, 0, 0) 模块watsup.winGuiAuto,如下例:

here

但是我相信最简单的方法是使用from watsup.winGuiAuto import * optDialog = findTopWindow(wantedText="File Upload") CancelButton = findControl(optDialog,wantedClass="Button", wantedText="Cancel") clickButton(SaveButton) autoit,我以前在pyqt中使用过它来发出命令。

希望这会有所帮助!

其他参考(pywin32版本):

here win32gui

[here win32api


1
投票

这里是一个纯PyQt4演示,或多或少地复制了默认实现:

here
© www.soinside.com 2019 - 2024. All rights reserved.