Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qml WebView runJavaScript problem with innerHTML
Forum Updated to NodeBB v4.3 + New Features

Qml WebView runJavaScript problem with innerHTML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 3.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Antonio Ortiz
    wrote on last edited by
    #1

    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?

    raven-worxR 1 Reply Last reply
    0
    • A Antonio Ortiz

      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?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Antonio-Ortiz
      on what type of elements are you calling innerHtml?
      When do you read the innerHtml 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 the innerHtml?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Antonio Ortiz
        wrote on last edited by Antonio Ortiz
        #3

        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

        raven-worxR 1 Reply Last reply
        0
        • A Antonio Ortiz

          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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Antonio-Ortiz
          shouldn't you use innerText instead, since you are querying a strong element?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Antonio Ortiz
            wrote on last edited by
            #5

            @raven-worx
            I tried and I got the same result. It prints an empty string

            raven-worxR JonBJ 2 Replies Last reply
            0
            • A Antonio Ortiz

              @raven-worx
              I tried and I got the same result. It prints an empty string

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @Antonio-Ortiz
              maybe you are executing your script too early?
              innerText does work.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • A Antonio Ortiz

                @raven-worx
                I tried and I got the same result. It prints an empty string

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Antonio-Ortiz
                If as @raven-worx says you are "too early", change your runJavaScript 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 the self.synchronousWebViewLoaded slot.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved