【发布时间】:2019-11-18 19:01:51
【问题描述】:
在了解了如何将 JavaScript 函数集成到 Qt-QML 中之后,我现在正在尝试将我在 my previous post 中学到的知识应用到我实验室中的一个小型实际示例中。
我正在尝试使用实验室中的第三方设备通过 wi-fi 访问我的机器人。
我有一个由设备提供的登录表格,如下面的打印屏幕所示。例如,地址是https://123.456.789.123:7878(这是我编的,但这是为了理解这个想法):
使用inspector tool 导航html 页面后,我到达了log in 按钮类,如下所示:
问题我现在正在尝试自动填充log-in 的密码并自动访问它。登录后我应该可以看到下面的页面,但我没有。
我应该提到我重用了Quick Nanao Browser Example,因为它在需要绕过需要认证的窗口时工作得很好。
示例
我在 Quick Nanao 浏览器示例上做了很多工作,以使其更简短,并准确地关注问题所在以及我试图解决它的方法。
我使用的代码 sn-p 如下:
main.cpp
#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;
}
return QUrl(QStringLiteral("https://123.456.789.123:7878"));
}
int main(int argc, char **argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QtWebEngine::initialize();
Application app(argc, argv);
QQmlApplicationEngine appEngine;
Utils utils;
appEngine.rootContext()->setContextProperty("utils", &utils);
appEngine.load(QUrl("qrc:/ApplicationRoot.qml"));
if (!appEngine.rootObjects().isEmpty())
QMetaObject::invokeMethod(appEngine.rootObjects().first(), "load", Q_ARG(QVariant, startupUrl()));
else
qFatal("Failed to load sources");
return app.exec();
}
ApplicationRoot.qml
import QtQuick 2.1
import QtWebEngine 1.9
QtObject {
id: root
property string script_password: "
setTimeout(function(){
var input_password = document.getElementsByName('password')[0];
input_password.value = '%1';
var button = document.getElementById('passwordNext');
button.click();
}, 100);
".arg(password)
property QtObject defaultProfile: WebEngineProfile {}
property QtObject otrProfile: WebEngineProfile {
offTheRecord: true
}
property Component browserWindowComponent: BrowserWindow {
applicationRoot: root
onClosing: destroy()
}
function createWindow(profile) {...}
function createDialog(profile) {...}
function load(url) {...}
}
BrowserWindow.qml
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.0
import QtQuick.Window 2.1
import QtWebEngine 1.9
ApplicationWindow {
id: browserWindow
property string password: "abcdefghilmno"
property QtObject applicationRoot
property Item currentWebView: tabs.currentIndex < tabs.count ? tabs.getTab(tabs.currentIndex).item : null
property int previousVisibility: Window.Windowed
width: 1300
height: 900
visible: true
title: currentWebView && currentWebView.title
Component.onCompleted: flags = flags | Qt.WindowFullscreenButtonHint
toolBar: ToolBar {...}
// Necessary to accept server's certificate
TabView {...}
// Showing Manager Window
WebEngineView {
id: devToolsView
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
onNewViewRequested: function(request) {
var tab = tabs.createEmptyTab(currentWebView.profile);
tabs.currentIndex = tabs.count - 1;
request.openIn(tab.item);
}
}
// By-Passing the Server's Certificate using sslDialog
MessageDialog {...}
DownloadView {
id: downloadView
visible: false
anchors.fill: parent
}
function onDownloadRequested(download) {
downloadView.visible = true;
downloadView.append(download);
download.accept();
}
}
非常感谢您就如何解决此问题以及如何继续提供一些指导。
【问题讨论】:
标签: javascript qt qml qt5 qwebengineview