mirror of https://github.com/vitalif/phantomjs
Bring back file upload support.
Instead of using command-line option, file can be uploaded using WebPage.uploadFile function, e.g.: var page = new WebPage(); page.open(....., function () { page.uploadFile('input#attachment', '/path/to/file'); }); uploadFile() accepts selector name (first argument), this is used to identify which input element will need the filename (second argument).1.2
parent
e998739fff
commit
47acc56153
|
@ -42,8 +42,13 @@ class CustomPage(QWebPage):
|
|||
self.parent = parent
|
||||
self.m_userAgent = QWebPage.userAgentForUrl(self, QUrl())
|
||||
|
||||
self.m_uploadFile = ''
|
||||
|
||||
do_action('CustomPageInit', Bunch(locals()))
|
||||
|
||||
def chooseFile(self, originatingFrame, oldFile):
|
||||
return self.m_uploadFile
|
||||
|
||||
def shouldInterruptJavaScript(self):
|
||||
QApplication.processEvents(QEventLoop.AllEvents, 42)
|
||||
return False
|
||||
|
@ -327,6 +332,21 @@ class WebPage(QObject):
|
|||
|
||||
return image.save(fileName)
|
||||
|
||||
@pyqtSlot(str, str)
|
||||
def uploadFile(self, selector, fileName):
|
||||
el = self.m_mainFrame.findFirstElement(selector)
|
||||
if el.isNull():
|
||||
return
|
||||
|
||||
self.m_webPage.m_uploadFile = fileName
|
||||
el.evaluateJavaScript('''
|
||||
(function (el) {
|
||||
var ev = document.createEvent('MouseEvents');
|
||||
ev.initEvent('click', true, true);
|
||||
el.dispatchEvent(ev);
|
||||
})(this)
|
||||
''')
|
||||
|
||||
@pyqtProperty('QVariantMap')
|
||||
def viewportSize(self):
|
||||
size = self.m_webPage.viewportSize()
|
||||
|
|
Loading…
Reference in New Issue