PDA

View Full Version : QWebView - How to get DOM HTML



lhg
16th October 2011, 23:59
So far I've been using "ui->browser->page()->mainFrame()->toHtml();"
It returns fine the static HTML, but several pages use javascript, therefore a lot of parts are looking like this "Loading..." instead of the actual information.
Is there anyway to get the DOM HTML?

The specific piece of HTML that I'm trying to get right now is below;


<tr class="data">
<td class="desc timer">
<span id="Countdown">Loading...</span>
</td>
</tr>


And the result I'm trying to get programatically would be something like this;


<tr class="data">
<td class="desc timer">
<span id="Countdown">2h 44m 18s</span>
</td>
</tr>


Thanks,
LuÃ*s.

Spitfire
17th October 2011, 15:51
Mybe you're just too quick and you're grabbing the html before JS has finished?

wysota
17th October 2011, 15:56
Use QWebFrame::findFirstElement() to query the DOM directly. Then you can traverse the tree using QWebElement API.

lhg
23rd October 2011, 19:16
@Spitfire this has nothing to do with it, the static HTML will never show any values updated by JS, it doesn't matter when you get it, the static html is "static". You have to check the DOM html to achieve this. But thanks for replying!

@wysota Although I was expecting an example, I was able to solve my problem quickly after you assured me that the right way would be using the WebElement API. So thanks, I've solved my problem.

A simple answer to my original poust would be:


QWebElement e = ui->browser->page()->mainFrame()->findFirstElement("span#Countdown");
qDebug() << e.toPlainText(); //a qDebug just to check it right now - it returns exactly the "2h 44m 18s" as I've mentioned above


Topic SOLVED.