Qt5和QML:如何使用WebEngine Quick Nano Browser自动输入用户名和密码

埃马努埃莱

我正在使用编写一个小型应用程序,QtQML使用Qt文档WebEngine Quick Nano Browser中的示例

在此示例中,我尝试访问我的电子邮件。我可以做到,但是我试图自动输入用户名和密码,以便我可以立即登录我的电子邮件。

基本上作为示例,启动应用程序后(我的电子邮件地址是硬编码的),我可以看到的username页面gmail,但是在这里我必须输入我的用户名才能访问具有password::的下一页。

用户名

在这里,我必须输入password

通过

Only after doing that I can access to my email.

The Expected result would be as soon as I run the application, I would like to go straight to my email without inputting username and password

The code that I am running is the following:

#include <QtGui/QGuiApplication>
typedef QGuiApplication Application;
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlContext>
#include <QtWebEngine/qtwebengineglobal.h>

static QUrl startupUrl()
{
    QUrl ret;
    QStringList args(qApp->arguments());
    args.takeFirst();
    for (const QString &arg : qAsConst(args)) {
        if (arg.startsWith(QLatin1Char('-')))
             continue;
        ret = Utils::fromUserInput(arg);
        if (ret.isValid())
            return ret;
    }

    // first email
    return QUrl(QStringLiteral("https://accounts.google.com/signin"));
}

int main(int argc, char **argv)
{
    QCoreApplication::setOrganizationName("QtExamples");
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    Application app(argc, argv);

    QtWebEngine::initialize();

    QQmlApplicationEngine appEngine;
    Utils utils;
    appEngine.rootContext()->setContextProperty("utils", &utils);
    appEngine.load(QUrl("qrc:/ApplicationRoot.qml"));
    QMetaObject::invokeMethod(appEngine.rootObjects().first(), "load", Q_ARG(QVariant, startupUrl()));

    return app.exec();
}

What I tried so far

  1. I have beed doing a lot of research and this source was useful in trying to understand the concept on how to hard code the entries. However, how do I proceed to the next passowrd page`?

  2. This is another example (although in php) of the same problem with a different approach. That also was useful to undertand the concept and the main idea.

  3. this additional source looks a very good example, although JavaScript is used here.

Now after reading much more information I found this post which makes me think that the best approach would be to integrate some JavaScript inside the QML code? I am a newbie in JavaScript if that would be the right route. And if this is the right thing, how can I do that?

I am considering to use QNetworkAccessManager in this application, but I am not sure how to best implement it.

Thanks to anyone who may have already gone through this or that may provide some guidance or an example to how implement this in the WebEngine Quick Nano Browser is very appreciated.

eyllanesc

Do not use the example WebEngine Quick Nano Browser as I see it unnecessary and only distract from the background objective.

逻辑是要了解Google登录的工作原理,在这种情况下,您必须找到一种方法来使用javascript获取DOM元素,例如通过id,class,name等,并根据所需的逻辑对其进行修改。还要注意,URL会更改,因此在每个URL中必须执行不同的脚本。

考虑到上述情况,解决方案是:

import QtQuick 2.13
import QtQuick.Controls 2.13
import QtWebEngine 1.9

ApplicationWindow{
    id: root
    width: 640
    height: 480
    visible: true

    property string username: "USERNAME"
    property string password: "PASSWORD"

    QtObject{
        id: internals
        property string script_username: "
            setTimeout(function() {
                var input_username = document.getElementById('identifierId');
                input_username.value = '%1';
                var button = document.getElementById('identifierNext');
                button.click();
            }, 1000);
        ".arg(username)

        property string script_password: "
            setTimeout(function() { 
                var input_password = document.getElementsByName('password')[0];
                input_password.value = '%1';
                var button = document.getElementById('passwordNext');
                button.click();
            }, 1000);
        ".arg(password)
    }

    WebEngineView {
        id: view
        anchors.fill: parent
        onUrlChanged: {
            if(url == "https://accounts.google.com/signin/v2/identifier?flowName=GlifWebSignIn&flowEntry=ServiceLogin"){
                view.runJavaScript(internals.script_username)   
            }
            else if(url == "https://accounts.google.com/signin/v2/sl/pwd?flowName=GlifWebSignIn&flowEntry=ServiceLogin&cid=1&navigationDirection=forward"){
                view.runJavaScript(internals.script_password)
            }
        }
        Component.onCompleted: view.url = "https://accounts.google.com/signin"
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法自动在“用户名”和“密码”输入字段中输入登录凭据

如何自动登录(不输入用户名和密码)(文本模式)

使FreeRDP提示用户输入用户名和密码?

如何删除html自动填充用户名和密码?

使用机械化输入用户名和密码

使用用户名和密码输入的JOptionPane

在iOS 12中自动填充用户名和密码

在UIWebView中自动填充用户名和密码

自动填充用户名和密码UIWebView Swift

Subversion提示输入用户名和密码

登录系统,输入用户名和密码后登录

输入Alamofire请求的用户名和密码

在输入字段中显示密码和用户名?

Live CD要求输入用户名和密码

输入用户名和密码时 UWP 崩溃

输入正确的用户名和密码后无法登录

使用appium自动执行登录过程时,密码和用户名会输入到用户名的同一字段中

Qt5 和 CMake:找不到 Quick、QuickControls 模块

当我输入错误的用户名或密码时,只会说**请输入用户名和密码**

首次登录Apache Airflow要求输入用户名和密码,用户名和密码是什么?

首次登录Apache Airlfow要求输入用户名和密码,用户名和密码是什么?

如何在提示连接到远程服务器时自动输入用户名和密码

在输入“用户名”和“密码”的同时显示文本,直到用户开始输入

用户名和密码检查

验证用户名和密码

注册用户名和密码

Android:存储用户名和密码?

JavaScript 密码和用户名验证

Jackrabitt:设置用户名和密码