请帮助我理解,如何操作 qt webkit 窗口中显示的 html 内容。我需要简单的操作,例如填写输入字段和单击按钮。有这方面的提示/文章吗?
最佳答案
请查看下面的示例。它使用 QWebView加载谷歌页面。然后使用 QWebFrame 类查找与搜索编辑框和“google 搜索”按钮对应的网页元素。编辑框会使用搜索查询进行更新,然后单击搜索按钮。
加载页面:
QWebView::connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(on_pageLoad_finished(bool)));
QUrl url("http://www.google.com/");
ui->webView->load(url);
on_pageLoad_finished 实现:
void MainWindow::on_pageLoad_finished(bool ok)
{
if (ok)
{
QWebFrame* frame = ui->webView->page()->currentFrame();
if (frame!=NULL)
{
// set google seatch box
QWebElementCollection collection = frame->findAllElements("input[name=q]");
foreach (QWebElement element, collection)
element.setAttribute("value", "how-to-manipulate-pages-content-inside-webkit-window-using-qt-and-qtwebkit");
// find search button
QWebElementCollection collection1 = frame->findAllElements("input[name=btnG]");
foreach (QWebElement element, collection1)
{
QPoint pos(element.geometry().center());
// send a mouse click event to the web page
QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(ui->webView->page(), &event0);
QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(ui->webView->page(), &event1);
}
}
}
}
希望这有帮助,问候
关于qt - 如何操作Webkit窗口内的页面内容(使用QT和QTWebKit)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2034981/