6

具有以下 html 的 DOM;

<a href="?op=order">
<img class="img_button" src="picture.gif"
  onMouseOver="this.src='some.gif';"
  onMouseOut="this.src='some_other.gif';"
  alt="" border="0">
</a>

如何跟踪 QWebKit(特别是 QWebPage)中的链接(href)。

请注意,这是一个链接的图像。
我不能通过模拟鼠标单击来做到这一点(即使可以,我也不想这样做),因为我不使用 QWebView 因此我没有呈现页面。

4

3 回答 3

8

假设您在名为“link”的变量中拥有链接的 QWebElement(通过 findFirst 或其他方式定位):

link.evaluateJavaScript("var evObj = document.createEvent('MouseEvents');evObj.initEvent('click', true, true);this.dispatchEvent(evObj);")

(这是在 Python 中,但重要的是 Javascript。是的,这是模拟鼠标点击,但由于它不使用坐标,因此它适用于未渲染的 QWebPage。)

于 2010-08-07T23:49:11.490 回答
4

Using DOM's click() Java Script function on the element makes the trick:

QWebPage * page = ...;
QWebElement el = page->mainFrame()->findFirstElement("a[href]");
el.evaluateJavaScript("this.click()"); 
于 2010-12-17T23:52:16.500 回答
2

如果您有 QWebView 并且不关心滚动自动化,这可能会有所帮助:

const QWebElement &element=__your_element__;
QWebView *view =__your_view__;
QWebFrame *const frame=view->page()->mainFrame();
QPoint const elemPos=element.geometry().center();
frame->setScrollPosition(elemPos);
QPoint const scrollPos=frame->scrollPosition();

QMouseEvent * const impossibleMousePress = new QMouseEvent(QEvent::MouseButtonPress,elemPos-scrollPos,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
QMouseEvent * const impossibleMouseRelease = new QMouseEvent(QEvent::MouseButtonRelease,elemPos-scrollPos,Qt::LeftButton,Qt::LeftButton,Qt::NoModifier);
QApplication::postEvent(view,impossibleMousePress);
QApplication::postEvent(view,impossibleMouseRelease);
于 2013-01-20T01:17:33.833 回答