我有一个简单的网页,我想提取文本My First Heading。
<!DOCTYPE html>
<html>
<script type="text/javascript">
//This is a single line comment
/* This is a
block comment */
</script>
<body>
<h1 id="title">My First Heading</h1>
<p id="text">My first paragraph.</p>
</body>
</html>
页面加载正常,但我的runJavascript函数不返回任何内容。 我尝试了不同的变化。我期望的调试输出应该是My First Heading。我错过了什么?
QString path("C:\\Temp\\x.html");
QFile file(path);
QString source;
file.open(QIODevice::ReadOnly);
source = file.readAll();
file.close();
page = new QWebEnginePage();
page->load(QUrl::fromLocalFile(path));
ui->widget->setPage(page);
ui->widget->setContextMenuPolicy(Qt::NoContextMenu);
//page->runJavaScript("document.getElementById(title)", [](const QVariant&result){ qDebug() << result.toString();});
//page->runJavaScript("document.getElementById(\"title\")", [](const QVariant&result){ qDebug() << result.toString();});
//page->runJavaScript("document.getElementById('title')", [](const QVariant&result){ qDebug() << result.toString();});