Qml WebView runJavaScript problem with innerHTML
-
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 theinnerHtmlproperty? 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
outerHtmlwhich also includes theinnerHtml? -
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
@Antonio-Ortiz
shouldn't you useinnerTextinstead, since you are querying astrongelement? -
@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 string@Antonio-Ortiz
If as @raven-worx says you are "too early", change yourrunJavaScriptto 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.synchronousWebViewLoadedslot.