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

Qml WebView runJavaScript problem with innerHTML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 3.8k Views
  • 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 5 Mar 2018, 22:05 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?

    R 1 Reply Last reply 6 Mar 2018, 13:57
    0
    • A Antonio Ortiz
      5 Mar 2018, 22:05

      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?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 6 Mar 2018, 13:57 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 6 Mar 2018, 16:04 last edited by Antonio Ortiz 3 Jun 2018, 16:04
        #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

        R 1 Reply Last reply 6 Mar 2018, 21:19
        0
        • A Antonio Ortiz
          6 Mar 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

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 6 Mar 2018, 21:19 last edited by raven-worx 3 Jun 2018, 21:19
          #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 6 Mar 2018, 22:54 last edited by
            #5

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

            R J 2 Replies Last reply 7 Mar 2018, 07:42
            0
            • A Antonio Ortiz
              6 Mar 2018, 22:54

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

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 7 Mar 2018, 07:42 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
                6 Mar 2018, 22:54

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

                J Offline
                J Offline
                JonB
                wrote on 7 Mar 2018, 09:37 last edited by JonB 3 Jul 2018, 09:39
                #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

                1/7

                5 Mar 2018, 22:05

                • Login

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