So let's say I'm trying to click a link in the QWebView, here is what I have:
// extending QWebView
void MyWebView::click(const QString &selectorQuery)
{
QWebElement el = this->page()->mainFrame()->findFirstElement(selectorQuery);
if (!el)
return;
el.setFocus();
QMouseEvent pressEvent(QMouseEvent::MouseButtonPress, el.geometry().center(),
Qt::MouseButton::LeftButton, Qt::LeftButton, Qt::NoModifier);
QCoreApplication::sendEvent(this, &pressEvent);
QMouseEvent releaseEvent(QMouseEvent::MouseButtonRelease,
el.geometry().center(), Qt::MouseButton::LeftButton,
Qt::LeftButton, Qt::NoModifier);
QCoreApplication::sendEvent(this, &releaseEvent);
}
And you call it as so:
myWebView->click("a[href]"); // will click first link on page
myWebView->click("input[type=submit]"); // submits a form
THE ONLY PROBLEM IS: if the element is not visible in the window, it is impossible to click. What I mean is if you have to scroll down to see it, you can't click it. I imagine this has to do with the geometry, since the element doesn't show up on the screen it can't do the math to click it right.
Any ideas to get around this? Maybe some way to make the window behave like a billion x billion pixels but still look 200x200?