Get Html element value with QWebEngine
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...
Code:
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??
Re: Get Html element value with QWebEngine
You could probably write a JavaScript snippet that returns the values you need and run it via http://doc.qt.io/qt-5/qwebenginepage...unJavaScript-1
Cheers,
_
Re: Get Html element value with QWebEngine
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().
Re: Get Html element value with QWebEngine
Quote:
Originally Posted by
anda_skoa
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?
Re: Get Html element value with QWebEngine
Quote:
Originally Posted by
danalex07
i believe thats not possible because runjavascript function returns void...
There is an overload that takes a result callback.
Cheers,
_
Re: Get Html element value with QWebEngine
Quote:
Originally Posted by
anda_skoa
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??
Re: Get Html element value with QWebEngine
The documentation says "functor or lambda" so it depends a bit if you have C++11 available for lambda.
Code:
runJavaScript
(script,
[] (const QVariant &result
) {// your callback code
});
As a functor
Code:
struct ScriptCallback
{
void operator
()(const QVariant &result
) { // your callback code
}
};
runJavaScript(script, ScriptCallback());
Cheers,
_
Re: Get Html element value with QWebEngine
thanks to anda_skoa i got this far...
Code:
webFrame->runJavaScript("function myFunction() {var myvar=document.querySelector('#GridView1 td:nth-child(3)');return myvar;} myFunction();",
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?
Re: Get Html element value with QWebEngine
Have you tried with a simple expression first?
E.g. a a fix return without a function, then within a function?
Cheers,
_
Re: Get Html element value with QWebEngine
Quote:
Originally Posted by
anda_skoa
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
Quote:
<table id="GridView1">
<tr>
<td>104534</td>
<td>04/04/2014</td>
<td>DM70F36L23CM</td>
</tr>
</table>
Re: Get Html element value with QWebEngine
Ok finally i got it...
for anyone interested in the code
Code:
webFrame->runJavaScript("function myFunction() {"
"var Row = document.getElementById('GridView1');var Cells = Row.getElementsByTagName('td');"
"return Cells[0].innerText;} myFunction();",
qDebug()<<result.toString();
});