pythonpyqt5qtwebengine

QWebEngineView and ignoring cert errors


I am trying to understand how this works and am struggling to figure out how to use it. The only examples I can find are not in Python and apparently I'm not that good at translating.

I've been digging through the help() results for most of these modules but am still not able to figure out how they work. Now if I read it right this should be able to be used to ignore a certificate error when loading a page.

QWebEngineCertificateError.ignoreCertificateError()

But when I try to run this I get the following error. I am pretty sure I am using it wrong but I can't find a good example of how its supposed to work.

TypeError: ignoreCertificateError(self): first argument of unbound method must have type 'QWebEngineCertificateError'

In a normal browser when you run into a cert error like this "ERR_CERT_AUTHORITY_INVALID" you can choose to proceed anyway. That option doesn't seem to be a default feature of QWebEngineView. What I am trying to do is implement that or to have it automatically just ignore the error and proceed.

Does anyone out there understand how to do this and are you willing to give me a pointer in the right direction? I'm stumped. I tried to get around the problem by just embedding a Chrome or edge browser into the app but it won't let my type in the browser though I can click on things and right click.

Here is an example code opening a website that triggers the same error. Its not what I need to load but its a site that triggers the same error.

from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
import sys

app = QApplication(sys.argv)
webview = QWebEngineView()
webview.load(QUrl("https://www.us.army.mil/"))
webview.show()
sys.exit(app.exec_())

screenshot of the error:

screenshot of the error


Solution

  • You have to override the certificateError() method of QWebEnginePage:

    import sys
    
    
    from PyQt5.QtCore import QUrl
    from PyQt5.QtWidgets import QApplication
    
    
    from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView
    
    
    class WebEnginePage(QWebEnginePage):
        def certificateError(self, error):
            # If you want to ignore the certificates of certain pages
            # then do something like
            # if error.url() == QUrl("https://www.us.army.mil/"):
            #     error.ignoreCertificateError()
            #     return True
            # return super().certificateError(error)
    
            error.ignoreCertificateError()
            return True
    
    
    def main(args):
        app = QApplication(args)
        webview = QWebEngineView()
        page = WebEnginePage()
        webview.setPage(page)
        webview.load(QUrl("https://www.us.army.mil/"))
        webview.show()
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        main(sys.argv)
    ```