python-3.xdomqwebviewonmouseclick

How do I get DOM element by mouse click on PyQT5 QWebView


I have simple application based on PyQT5. I need to get DOM element that is under mouse cursor when the mouse button is clicked.


Solution

  • Ok. Will answer myself:

    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
    
            self.view = QWebView(self)
            self.view.installEventFilter(self)
            # create other components here
    
    
        def eventFilter(self, obj, event):
            if obj == self.view:
                if (event.type() == QEvent.MouseButtonRelease):
                    htc = self.view.page().mainFrame().hitTestContent(event.pos())
                    e = htc.element()
                    if e:
                        #do somesing with e
                        return True
            return  QMainWindow.eventFilter(obj, event)