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

前端 未结 1 2034
时光说笑
时光说笑 2021-02-06 15:58

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 butt

相关标签:
1条回答
  • 2021-02-06 16:34

    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 讨论(0)
提交回复
热议问题