Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to follow a link in QWebKit?

Having a DOM of the following html;

<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>

how to follow a link (href) in QWebKit (specifically QWebPage).

Please notice that it's an image that is linked.
I can't do it (and I don't want to even if I could) by simulating a mouse click as I don't use QWebView thus I don't have the page rendered.

like image 836
Piotr Dobrogost Avatar asked Aug 02 '09 21:08

Piotr Dobrogost


2 Answers

Assuming you have the link's QWebElement in a variable called "link" (located through findFirst or whatever):

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

(This is in Python, but it is the Javascript that matters. And yes, this is simulating a mouse click, but since it does not use coordinates, it works fine with an unrendered QWebPage.)

like image 63
Mikael Avatar answered Sep 22 '22 13:09

Mikael


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()"); 
like image 35
Piotr Dobrogost Avatar answered Sep 21 '22 13:09

Piotr Dobrogost