[PyQt] PyQt bug: choose file dialog sometimes opens instead of QWebPage.chooseFile call

Герасимов Михаил Gerasimov-M-N at yandex.ru
Wed May 14 05:54:00 BST 2014


Hello! Sorry for my broken english.

I emulate click on file upload button in QWebView to automate file 
uploading. On some sites instead of QWebPage.chooseFile function call, 
Windows choose file dialog opens (even if QWebView is hidden). I'm using 
PyQt5.2.1, Windows 7 ×86, python 3.3.

Is it PyQt bug? Should I report it somewhere? Thanks for help.

Here is code example:


import sys

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebKitWidgets import QWebView, QWebPage
from PyQt5.Qt import Qt
from PyQt5.QtGui import QMouseEvent


class CustomQWebView(QWebView):
     def __init__(self):
         QWebView.__init__(self)
         self.setPage(CustomQWebPage())

         self._test_url = "http://postimage.org/"
         self._upload_selector = '#uploadifive-file_upload'

     def start_test(self):
         self.page().mainFrame().loadFinished.connect(self._on_ready)
         self.setUrl(QUrl(self._test_url))
         self.show()

     def _on_ready(self, is_ok):
         el = 
self.page().mainFrame().findFirstElement(self._upload_selector)

         # emulate click on element
         el_pos = el.geometry().center()
         self.page().mainFrame().setScrollPosition(el_pos)
         scr_pos = self.page().mainFrame().scrollPosition()
         point_to_click = el_pos - scr_pos

         press = QMouseEvent(QMouseEvent.MouseButtonPress, 
point_to_click, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
         release = QMouseEvent(QMouseEvent.MouseButtonRelease, 
point_to_click, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
         QApplication.postEvent(self, press)
         QApplication.postEvent(self, release)

         # after this click choose file dialog opens (sometimes)


class CustomQWebPage(QWebPage):
     def chooseFile(self, frame, suggested_file=None):
         print('chooseFile call')
         return 'some_file_name.png'


if __name__ == '__main__':
     app = QApplication(sys.argv)

     web = CustomQWebView()
     web.start_test()

     sys.exit(app.exec_())





More information about the PyQt mailing list