pythonpyqtpyqt4qwebview

TypeError: javaScriptConfirm() takes exactly 1 argument (3 given) python


self.browser = QWebView()
self.page = MyWebPage()
self.browser.setPage(self.page)

the MyWebPage() class is:

class MyWebPage(QWebPage):
    def javaScriptAlert(self):
        pass
    def javaScriptConfirm(self):
        pass
    def javaScriptConsoleMessage(self):
        pass
    def javaScriptPrompt(self):
        pass

then I get the type error:

TypeError: javaScriptConfirm() takes exactly 1 argument (3 given)

can anyone tell me why I am getting that error?


Solution

  • If you want to overwrite some method of a class the function must have the same number of arguments, if we review the docs:

    virtual void javaScriptAlert(QWebFrame * frame, const QString & msg)

    virtual bool javaScriptConfirm(QWebFrame * frame, const QString & msg)

    virtual void javaScriptConsoleMessage(const QString & message, int lineNumber, const QString & sourceID)

    virtual bool javaScriptPrompt(QWebFrame * frame, const QString & msg, const QString & defaultValue, QString * result)

    by reviewing you must overwrite the methods as follows:

    class MyWebPage(QWebPage):
        def javaScriptAlert(self, frame, msg):
            pass
        def javaScriptConfirm(self, frame, msg):
            pass
        def javaScriptConsoleMessage(self, message, lineNumber, sourceID):
            pass
        def javaScriptPrompt(self, frame, msg, defaultValue, result):
            pass
    

    Or if you are not going to use any of those parameters you could use *args and **kwargs:

    class MyWebPage(QWebPage):
        def javaScriptAlert(self, *args, **kwargs):
            pass
        def javaScriptConfirm(self, *args, **kwargs):
            pass
        def javaScriptConsoleMessage(self, *args, **kwargs):
            pass
        def javaScriptPrompt(self, *args, **kwargs):
            pass