我有一个简单的网页,我想提取我的第一个标题的文本。
<!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函数没有返回任何内容。我尝试过不同的变种。我期望的调试输出应该是我的第一个标题。我遗漏了什么?
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();});发布于 2017-08-10 22:36:26
文档说,当页面内容完全加载时,会发出一个信号:loadFinished。所以我认为你应该把这个信号连接到一个自定义的插槽,只有在调用了这个插槽之后,你才能访问页面的内容。
https://stackoverflow.com/questions/45616469
复制相似问题