1

What I am ultimately trying to accomplish is to capture the username and password that the user enters into a website. For example, if the user enters "[email protected]" as the email address into Facebook's login, then clicks submit, I want to store that email address in my PyQt Application.

The closest I've come to achieving this has been using a series of JavaScript commands to place a listener on the "Login Button" that returns the current value of the user parameter. My problem is that the callback that PyQt provides is for when the runJavaScript function is completed, not the javascript event listener. I'm wondering if there is any way to capture the callback function from the JavaScript function, or if there is a better way altogether for me to do this.

import os
import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtCore import QUrl, QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView

class WebPage(QWebEngineView):
    def __init__(self):
        QWebEngineView.__init__(self)
        self.load(QUrl("https://facebook.com"))
        self.loadFinished.connect(self._on_load_finished)
        #self.page().runJavaScript("document.getElementById("myBtn").addEventListener("click", displayDate)", print)

    def _on_load_finished(self):
        print("Finished Loading")
        cmds = ["btn=document.getElementById('u_0_r')",  # Login Button
                "user=document.getElementsByName('email')[0]",
                "function get_username(){return user.value}",
                "btn.addEventListener('click', get_username)"]
        self.page().runJavaScript("; ".join(cmds), lambda x: print("test: %s"  % x))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    web = WebPage()
    web.show()
    sys.exit(app.exec_())  # only need one app, one running event loop
4
  • You're trying to capture the username and password ?? Commented Jul 21, 2017 at 18:22
  • Yes, the example just shows username/email because (correct me if I'm wrong) the principles should be the same Commented Jul 21, 2017 at 18:39
  • But you appear to be trying to steal the users' passwords... Commented Jul 21, 2017 at 18:41
  • @ekhumoro Less about stealing and more about remembering / auto-filling the user's password each time they enter the browser. Think dashlane/lastpass/chrome-autofill. Happy to take other suggestions if you have them Commented Jul 22, 2017 at 0:05

1 Answer 1

2

I found a work around using the "urlChanged" signal that seems to work so far for my applications

import os
import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtCore import QUrl, QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor

class WebPage(QWebEngineView):
    def __init__(self):
        QWebEngineView.__init__(self)
        self.current_url = ''
        self.load(QUrl("https://facebook.com"))
        self.loadFinished.connect(self._on_load_finished)
        self.urlChanged.connect(self._on_url_change)

    def _on_load_finished(self):
        self.current_url = self.url().toString()

    def _on_url_change(self):
        self.page().runJavaScript("document.getElementsByName('email')[0].value", self.store_value)

    def store_value(self, param):
        self.value = param
        print("Param: " +str(param))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    web = WebPage()
    web.show()
    sys.exit(app.exec_())  # only need one app, one running event loop
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.