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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With