开发者

How to manipulate pages content inside Webkit window (Using QT and QTWebKit)?

Please help me to understand, how can I manipulate 开发者_如何学Gohtml 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 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.

Load page:

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

hope this helps, regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜