Qt: force QWebView clicks on the web element, even one does not appear in the window

So let's say I'm trying to click a link in a 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 that:

 myWebView->click("a[href]"); // will click first link on page myWebView->click("input[type=submit]"); // submits a form 

PROBLEM ONLY: if the item does not appear in the window, you cannot click. . I mean, if you need to scroll down to see it, you cannot click It. I suppose this is due to geometry, since the element is not displayed on the screen, it cannot do the math to click on it correctly.

Any ideas to get around this? Maybe some way to make the window behave like a billion x billion pixels, but it still looks 200x200?

+7
c ++ qt qtwebkit qwebview
source share
3 answers

I think calling el.evaluateJavaScript("click()"); must work. I say it should work because in the past I have successfully used QWebElement::function() with the argument "click". However, this method did not become part of the QWebElement API. I think the authors concluded that this was redundant in the presence of QWebElement::evaluateJavaScript() .

My similar question - How to follow the link in QWebKit? - still unanswered :( I came up with the same workaround as you are here - Problem evaluating JavaScript ()

+7
source share

Hi, I know this question is quite old, but I wanted to offer a different answer. You said: "... since the item is not displayed on the screen, it cannot do the math to click it ...". Well, “ he ” cannot do the math, but “ You ” can .;) You can get the position of the element you want to click and calculate how many pixels ( X ) you need to scroll up / down to make it visible. Then you can automatically scroll the page using X pixels and use the X value to calculate the current position of the element and click there. The same logic is used if you need to scroll left or right. Just use Y-pixels as the horizontal correction value. I did it in another language, and I know that it is doable and works fine. :)

EDIT: (February 21, 2013)

OK, I had to do this for one of my projects. So, to illustrate what I had in mind above, below is the Qt code. I understand that it has some disadvantages, especially if you are dealing with small elements around the edges of the page, but in most cases this works fine.

(in the code below, * elemt is the item you want to click and * QWebView web widget)

 QRect elGeom=element->geometry(); QPoint elPoint=elGeom.center(); int elX=elPoint.x(); int elY=elPoint.y(); int webWidth=web->width(); int webHeight=web->height(); int pixelsToScrolRight=0; int pixelsToScrolDown=0; if (elX>webWidth) pixelsToScrolRight=elX-webWidth+elGeom.width()/2+10; //the +10 part if for the page to scroll a bit further if (elY>webHeight) pixelsToScrolDown=elY-webHeight+elGeom.height()/2+10; //the +10 part if for the page to scroll a bit further web->page()->mainFrame()->setScrollBarValue(Qt::Horizontal,pixelsToScrolRight); web->page()->mainFrame()->setScrollBarValue(Qt::Vertical,pixelsToScrolDown); QPoint pointToClick(elX-pixelsToScrolRight,elY-pixelsToScrolDown); QMouseEvent pressEvent(QMouseEvent::MouseButtonPress,pointToClick,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier); QCoreApplication::sendEvent(web, &pressEvent); QMouseEvent releaseEvent(QMouseEvent::MouseButtonRelease,pointToClick,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier); QCoreApplication::sendEvent(web, &releaseEvent); 

This is what works for me when the java click failed. I hope it will be useful for someone else.

+7
source share

For those who want to click the Web View button, follow these steps

 frame1->evaluateJavaScript( "document.forms[0].submit();" ); 
+1
source share

All Articles