qt - 如何操作Webkit窗口内的页面内容(使用QT和QTWebKit)?

标签 qt macros webkit

请帮助我理解,如何操作 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/

相关文章:

css - 形状外和垂直对齐的文本

javascript - jQuery .focus() 和 .blur() 在 Chrome 或 Safari 中不起作用

c++ - 映射多变量/层系统的泛化

c++ - 访问 QApplication::arguments 时出现段错误

c++ - 复制到 QMap 时出现 QString 错误。范围问题?

c++ - MACRO 取决于其文件夹位置

python - QLineEdit 上的 QCompleter 用于部分插入的文本

c++ - 使用宏在本地禁用 qDebug 输出

c++ - 条件宏文本替换

.net - 如何从 Mono 的 WebKitSharp 调用 JavaScript?