我正在使用编写一个小型应用程序,Qt
并QML
使用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
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`?
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.
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.
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] 删除。
我来说两句