如何在Webkit窗口中操作页面内容(使用QT和QTWebKit)? [英] How to manipulate pages content inside Webkit window (Using QT and QTWebKit)?

查看:343
本文介绍了如何在Webkit窗口中操作页面内容(使用QT和QTWebKit)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我理解如何处理qt webkit窗口中显示的html内容.我需要简单的操作,例如填写输入字段并单击一个按钮.有任何提示/文章吗?

Please help me to understand, how can I manipulate html content which is shown inside qt webkit window. I need simple operations like filling in input fields and clicking a button. Any tips/article on this?

推荐答案

pls请查看下面的示例.它使用 QWebView 加载Google页面.然后使用QWebFrame类查找与搜索编辑框和"google搜索"按钮相对应的Web元素.使用搜索查询更新编辑"框,然后单击搜索按钮.

pls check an example below. It uses QWebView to load the google page. Then uses QWebFrame class to find web elements corresponding to the search edit box and "google search" button. Edit box gets updated with a search query and then the search button gets clicked.

加载页面:

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实现:

on_pageLoad_finished implementation:

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);
            }
        }
    }
}

希望这会有所帮助

这篇关于如何在Webkit窗口中操作页面内容(使用QT和QTWebKit)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆