PDA

View Full Version : Get Html element value with QWebEngine



danalex07
30th January 2016, 20:46
Im porting my QWebkit based app to QWebEngine but im having trouble getting some value of the Html page. this was easily done this way...



QWebFrame *webFrame = ui->webView->page()->mainFrame();
QWebElement variable;
variable= webFrame->findChild("#GridView1 td:nth-child(1)");


but i´ve just read that QWebElement need a rework and it is no included...so my question is: how can i get elements values from the HTML page in a similar way of old Webkit module??

anda_skoa
31st January 2016, 09:56
You could probably write a JavaScript snippet that returns the values you need and run it via http://doc.qt.io/qt-5/qwebenginepage.html#runJavaScript-1

Cheers,
_

code_err
31st January 2016, 11:14
I'm not sure but maybe it is a matter of wrong syntax. I was using QWebElement to get Html elements and it worked but I used much simpler expressions. You have to try. I don't remember but I probably had problems with using # sign.
There is also QTextDocument, you can find element in QString with regular expressions and then put it into QTextDocument and use method toPlainText().

danalex07
2nd February 2016, 03:10
You could probably write a JavaScript snippet that returns the values you need and run it via http://doc.qt.io/qt-5/qwebenginepage.html#runJavaScript-1

Cheers,
_

i believe thats not possible because runjavascript function returns void...

i believe that get the text can be accomplished by using QWebEnginePage selectedText() this function returns a QString of the current selected text, next step is to virtually select the text using javascript and iterate over all table cells but i'm stuck in the part of set the cell value as selected...any ideas?

anda_skoa
2nd February 2016, 08:02
i believe thats not possible because runjavascript function returns void...

There is an overload that takes a result callback.

Cheers,
_

danalex07
3rd February 2016, 02:58
There is an overload that takes a result callback.

Cheers,
_

Ohhhhh yes but i cant make it work, function pointers in Qt make me cry...can u help me a bit??

anda_skoa
3rd February 2016, 09:15
The documentation says "functor or lambda" so it depends a bit if you have C++11 available for lambda.


runJavaScript(script, [] (const QVariant &result) {
// your callback code
});


As a functor


struct ScriptCallback
{
void operator()(const QVariant &result) {
// your callback code
}
};

runJavaScript(script, ScriptCallback());


Cheers,
_

danalex07
4th February 2016, 00:59
thanks to anda_skoa i got this far...



webFrame->runJavaScript("function myFunction() {var myvar=document.querySelector('#GridView1 td:nth-child(3)');return myvar;} myFunction();",
[] (const QVariant &result) {
qDebug()<<result;
});



but i'm stuck know in js because it doesn't return the cell value i'm pointing to with the querySelector...maybe wrong syntaxis?

anda_skoa
4th February 2016, 09:28
Have you tried with a simple expression first?
E.g. a a fix return without a function, then within a function?

Cheers,
_

danalex07
4th February 2016, 17:27
Have you tried with a simple expression first?
E.g. a a fix return without a function, then within a function?

Cheers,
_


yes simple function like "return 3.4" or "function myFunction() {return 3.4;} myFunction();" works fine, but i can't just find a javascript functions that saves table cell value


<table id="GridView1">
<tr>
<td>104534</td>
<td>04/04/2014</td>
<td>DM70F36L23CM</td>
</tr>
</table>

danalex07
5th February 2016, 03:18
Ok finally i got it...

for anyone interested in the code





webFrame->runJavaScript("function myFunction() {"
"var Row = document.getElementById('GridView1');var Cells = Row.getElementsByTagName('td');"
"return Cells[0].innerText;} myFunction();",
[] (const QVariant &result) {
qDebug()<<result.toString();
});