Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

macros

webkit

qt

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?

like image 857
Andersson83 Avatar asked Feb 27 '23 19:02

Andersson83


1 Answers

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

like image 63
serge_gubenko Avatar answered Apr 06 '23 10:04

serge_gubenko