HTML内容:
<html> <head> </head> <body> <test id="test1" value="this is test." ></test> </body> </html>
Qt代码:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QWebEngineView> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); this->w = new QWebEngineView(this); this->w->setUrl(QUrl("file:///D:/TMP/qt/1.html")); this->w->show(); // connect(this->w,SIGNAL(loadFinished(bool)), this,SLOT(on_web_loadFinished(bool))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_web_loadFinished(bool) { this->w->page()->runJavaScript("document.getElementById(\"test1\").getAttribute(\"value\")",[]( const QVariant &v ){ qDebug()<<v<<endl; }); this->w->page()->runJavaScript("document.evaluate(\".//test\", document.body, null, XPathResult.ANY_TYPE, null).iterateNext().getAttribute(\"value\")",[]( const QVariant &v ){ qDebug()<<v<<endl; }); }
效果:
QMetaObject::connectSlotsByName: No matching signal for on_web_loadFinished(bool) [6944:5192:0816/112807.262:INFO:media_foundation_video_encode_accelerator_win.cc(370)] Windows versions earlier than 8 are not supported. [6944:5192:0816/112807.262:INFO:media_foundation_video_encode_accelerator_win.cc(370)] Windows versions earlier than 8 are not supported. QVariant(QString, "this is test.") QVariant(QString, "this is test.")