简体   繁体   English

从 QWebEngineView 自动登录到网站

[英]Automatic Login from QWebEngineView to Website

I am trying to use the code below to create a view of a monday.com in a PySide2 app.我正在尝试使用下面的代码在 PySide2 应用程序中创建 monday.com 的视图。 I want to be able to login directly from the code instead of having to input my email and password.我希望能够直接从代码登录,而不必输入我的电子邮件和密码。 I have found some code pretty similar to the one shown below and adapt it to how monday.com's login page is structured.我发现了一些与下面显示的代码非常相似的代码,并将其调整为 monday.com 登录页面的结构。

I am currently able to enter my email and password as well as to click the login button.我目前可以输入我的电子邮件和密码以及单击登录按钮。 But, for some reason, it appears as if the page did not detect the entries although you can visually see them.但是,出于某种原因,尽管您可以直观地看到它们,但页面似乎没有检测到这些条目。

I've tried to put a second function that runs after "handle_load_finished" and included the JavaScript for clicking the button there but it would still run and not detect that entries were there.我试图放置在“handle_load_finished”之后运行的第二个函数,并包含用于单击那里的按钮的 JavaScript,但它仍然会运行并且不会检测到那里的条目。

from PySide2.QtCore import QUrl
from PySide2.QtWidgets import QMainWindow, QApplication
from PySide2.QtWebEngineWidgets import QWebEngineView

import os
import sys


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl("https://miu2021.monday.com/auth/login_monday/email_password"))
        self.setCentralWidget(self.browser)

        self.browser.loadFinished.connect(self.handle_load_finished)

    def handle_load_finished(self, ok):
        if ok:
            print("Page loaded successfully")
            self.browser.page().runJavaScript("let ap = document.querySelector('#user_email');"
                                              "ap.value = 'email@email.com';"
                                              "let pa = document.querySelector('#user_password');"
                                              "pa.value = 'password';"
                                              "let btn = document.getElementsByTagName('button')[0];"
                                              "btn.click();")
        else:
            print("Could not load page")


app = QApplication(sys.argv)
window = MainWindow()
window.show()

app.exec_()

App Image:应用图片:

应用图片

It seems that the website detects the touch to verify that the user types and is not an automated task.该网站似乎检测到触摸以验证用户是否键入并且不是自动任务。 A possible solution is to make Qt write by sending the QKeyEvents as I show below.一个可能的解决方案是通过发送 QKeyEvents 来使 Qt 写入,如下所示。

import sys

from PySide2.QtCore import QCoreApplication, QEvent, Qt, QTimer, QUrl
from PySide2.QtGui import QKeyEvent
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtWebEngineWidgets import QWebEngineView

KEYMAP = {
    "a": Qt.Key_A,
    "b": Qt.Key_B,
    "c": Qt.Key_C,
    "d": Qt.Key_D,
    "e": Qt.Key_E,
    "f": Qt.Key_F,
    "g": Qt.Key_G,
    "h": Qt.Key_H,
    "i": Qt.Key_I,
    "j": Qt.Key_J,
    "k": Qt.Key_K,
    "l": Qt.Key_L,
    "m": Qt.Key_M,
    "n": Qt.Key_N,
    "o": Qt.Key_O,
    "p": Qt.Key_P,
    "q": Qt.Key_Q,
    "r": Qt.Key_R,
    "s": Qt.Key_S,
    "t": Qt.Key_T,
    "u": Qt.Key_U,
    "v": Qt.Key_V,
    "w": Qt.Key_W,
    "x": Qt.Key_X,
    "y": Qt.Key_Y,
    "z": Qt.Key_Z,
    "1": Qt.Key_1,
    "2": Qt.Key_2,
    "3": Qt.Key_3,
    "4": Qt.Key_4,
    "5": Qt.Key_5,
    "6": Qt.Key_6,
    "7": Qt.Key_7,
    "8": Qt.Key_8,
    "9": Qt.Key_9,
    "0": Qt.Key_0,
    ".": Qt.Key_Period,
    "'": Qt.Key_Apostrophe,
}


def write_text(widget, text):
    for letter in text:
        key = KEYMAP.get(letter.lower(), Qt.Key_unknown)
        event = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier, letter)
        QCoreApplication.postEvent(widget.focusProxy(), event)


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.browser = QWebEngineView()
        self.browser.load(
            QUrl("https://miu2021.monday.com/auth/login_monday/email_password")
        )
        self.setCentralWidget(self.browser)

        self.browser.loadFinished.connect(self.handle_load_finished)

    def handle_load_finished(self, ok):
        if ok:
            QTimer.singleShot(100, self.write_email)
        else:
            print("Could not load page")

    def write_email(self, *args):
        self.browser.page().runJavaScript(
            """let ap = document.querySelector('#user_email')
                ap.focus();""",
            0,
            self.callback_email,
        )

    def callback_email(self, *args):
        write_text(self.browser, "email@email.com")
        QTimer.singleShot(100, self.write_password)

    def write_password(self):
        self.browser.page().runJavaScript(
            """let pa = document.querySelector('#user_password');
                pa.focus();""",
            0,
            self.callback_password,
        )

    def callback_password(self, *args):
        write_text(self.browser, "password")
        QTimer.singleShot(100, self.click)

    def click(self):
        self.browser.page().runJavaScript(
            """let btn = document.getElementsByTagName('button')[0];
                btn.focus();
                btn.click();"""
        )


def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM