Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>setAttribute</code> method might still not work for security reasons. </p> <p>But you can redefine the function <code>QWebPage::chooseFile</code> that should normally open the upload dialog and return the filename so that it returns a static file name without opening the dialog, and activate that upload by simulating a "return" key press on the input element.</p> <p>This seems to work:</p> <pre><code>from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtWebKit import * import sys class WebPage(QWebPage): def __init__(self, parent = None): super(WebPage, self).__init__(parent) self.overrideUpload = None def chooseFile(self, originatingFrame, oldFile): if self.overrideUpload is None: return super(WebPage, self).chooseFile(originatingFrame, oldFile) result = self.overrideUpload self.overrideUpload = None return result def setUploadFile(self, selector, filename): button = self.mainFrame().documentElement().findFirst(selector) self.overrideUpload = filename # set the focus on the input element button.setFocus(); # and simulate a keypress event to make it call our chooseFile method webview.event(QKeyEvent(QEvent.KeyPress, Qt.Key_Enter, Qt.NoModifier)) def upload(): print 'uploading...' page.setUploadFile('input[id="photos_upload_input"]', '/Users/elmigranto/Downloads/stuff.png') # The change seems to be asynchronous, at it isn't visible # just after the previous call app = QApplication(sys.argv) webview = QWebView() page = WebPage(webview) webview.setPage(page) source = ''' &lt;form action="#"&gt; Select a file: &lt;input type="file" id="photos_upload_input"&gt; &lt;input type="submit"&gt; &lt;/form&gt; ''' webview.loadFinished.connect(upload) webview.show() webview.setHtml(source) sys.exit(app.exec_()) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload