1

So, the question is on using QWebEngineView or QWebEnginePage, or something like that. I have a link which looks like <a href="/salt">Salt</a>. I have already managed to intercept clicking on this link and force QWebEngineView to do nothing with it because all that I need is to print the text of this url ("Salt") in the python console instead of opening it. The problem is that I haven't any idea how to implement it. There was linkHovered() signal in PyQt4 which returns the title, text and url of the link when the mouse hovers over it. In PyQt5 it returns only the url. Any ideas?

Some code:

class MyWebEnginePage(QWebEnginePage):
    link_clicked = pyqtSignal(str)
    def acceptNavigationRequest(self, url, nav_type, is_frame):
        if nav_type == 0:
            self.link_clicked.emit(str(url))
            return False
        else:
            return True

self.page.link_clicked.connect(self.LEchange)

def LEchange(self,url):
    print(url)

It prints:

PyQt5.QtCore.QUrl('file:///C:/salt')

but if the link looks like <a href="/salt">Salt</a> I need to get "Salt".

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
V.Nosov
  • 93
  • 3
  • 10
  • Salt is just an example, links actually looks like Катерпиллар or Государство (words aren't equal) – V.Nosov Oct 23 '17 at 15:03
  • None of this is possible with `QWebEngine`. Everything happens aynchronously, and there is no direct access to the page elements at all. The only way you can interact with the html/dom is via javascript - but the web-page itself cannot tell you which element was clicked, so that is not much help. One thing you have to understand about `QWebEngine` is that it is not a drop-in replacement for `QWebKit`. There are many things that have not been implemented yet, and some that probably never will be. – ekhumoro Oct 23 '17 at 17:13
  • But if I will use PyQt4 I will be able to do this via linkHovered, won’t I? – V.Nosov Oct 23 '17 at 19:01
  • Yes, but PyQt4 only has webkit, not webengine. With webengine, the only way to get information about a clicked html/dom element is by using javascript. – ekhumoro Oct 23 '17 at 19:11
  • I've understand, thx. JS is not my strong side. But migration from PyQt5 to PyQt4 doesn't look like a very hard task since I've used only few functions which according to Qt documentation differ only in name and type of returned values. Actually, i'm going to change `QWebEngineView` to `QWebView` (and ~Page to QWebPage). Hope it will work. – V.Nosov Oct 23 '17 at 19:25

0 Answers0