tags:

views:

614

answers:

2

Hello!

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?

+1  A: 

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 seatch 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

serge_gubenko
A: 

hi how can i hold a reference to the google page of results after QApplication::sendEvent(ui->webView->page(), &event1) ? i need to have the content of the page thet normaly user whould see after clicking Thanks.

meirdrago