Qml WebView runJavaScript problem with innerHTML
-
wrote on 5 Mar 2018, 22:05 last edited by
Hi, I'm trying to get the innerHTML of an element of a webpage that I'm loading using a qml WebView. I use runJavaScript() method to get the innerHTML with the following:
var js = "document.getElementsByClassName(\"ng-binding\")[6].innerHTML;"; webView.runJavaScript(js, function(result) { console.log(result); });;
But result is an empty string. I tried out the same Javascript line in the browser and it shows the inner html.
The curious thing is that if I run in qml:
var js = "document.getElementsByClassName(\"ng-binding\")[6].outerHTML;"; webView.runJavaScript(js, function(result) { console.log(result); });;
It prints the outer html without problem.
Anyone knows why innerHTML is showing an empty string? -
Hi, I'm trying to get the innerHTML of an element of a webpage that I'm loading using a qml WebView. I use runJavaScript() method to get the innerHTML with the following:
var js = "document.getElementsByClassName(\"ng-binding\")[6].innerHTML;"; webView.runJavaScript(js, function(result) { console.log(result); });;
But result is an empty string. I tried out the same Javascript line in the browser and it shows the inner html.
The curious thing is that if I run in qml:
var js = "document.getElementsByClassName(\"ng-binding\")[6].outerHTML;"; webView.runJavaScript(js, function(result) { console.log(result); });;
It prints the outer html without problem.
Anyone knows why innerHTML is showing an empty string?@Antonio-Ortiz
on what type of elements are you callinginnerHtml
?
When do you read theinnerHtml
property? Since you are querying for "ng-binding" i assume you are using AngularJS?@Antonio-Ortiz said in Qml WebView runJavaScript problem with innerHTML:
It prints the outer html without problem.
You mean you get successfully the
outerHtml
which also includes theinnerHtml
? -
wrote on 6 Mar 2018, 16:04 last edited by Antonio Ortiz 3 Jun 2018, 16:04
Hi @raven-worx , thanks for your quick respond.... the element is a <strong>, I read the innerHTML property after the webview progress reach 100, yes the webpage use Angular.js and when I print the outerHTML it doesn't print the innerHTML
-
Hi @raven-worx , thanks for your quick respond.... the element is a <strong>, I read the innerHTML property after the webview progress reach 100, yes the webpage use Angular.js and when I print the outerHTML it doesn't print the innerHTML
Moderatorswrote on 6 Mar 2018, 21:19 last edited by raven-worx 3 Jun 2018, 21:19@Antonio-Ortiz
shouldn't you useinnerText
instead, since you are querying astrong
element? -
wrote on 6 Mar 2018, 22:54 last edited by
@raven-worx
I tried and I got the same result. It prints an empty string -
@raven-worx
I tried and I got the same result. It prints an empty string@Antonio-Ortiz
maybe you are executing your script too early?
innerText does work. -
@raven-worx
I tried and I got the same result. It prints an empty stringwrote on 7 Mar 2018, 09:37 last edited by JonB 3 Jul 2018, 09:39@Antonio-Ortiz
If as @raven-worx says you are "too early", change yourrunJavaScript
to do its work on a "content-ready" or "load-finished" JS event. The document isn't fully ready/loaded till these events are raised, so you might be examining the content too early.Indeed, in my own code before I call
runJavaScript()
I use:self.webView.loadFinished.connect(self.synchronousWebViewLoaded)
and call the
runJavaScript()
in theself.synchronousWebViewLoaded
slot.
1/7