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.
dv8
source share