Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Webelement click does not work
Forum Updated to NodeBB v4.3 + New Features

Webelement click does not work

Scheduled Pinned Locked Moved QtWebEngine
14 Posts 3 Posters 7.5k 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.
  • F fgdevel

    Hello,

    I have a HTML page (HTML/CSS/Javascript) with :

    [ HTML ]
    <li id="tool_open" >Open</li>

    [ Javascript ]
    document.getElementById('tool_open').click();

    It works !

    I try to do the same in Qt (WebEngine) :
    [ Qt ]
    QWebEngineView * view = new QWebEngineView;
    QWebPage * page;
    QWebView *view2;
    QWebFrame * frame;
    QWebElement document;
    QWebElementCollection elements;
    [ ... ]
    document = frame->documentElement();
    elements = document.findAll("li");

    foreach (QWebElement element, elements){
          if (element.attribute("id") == "tool_open")
        {
           // test 1 : KO
           element.evaluateJavaScript("this.click()"); 
    
           // test 2 : KO                    
    document.findFirst("li[id='tool_open']").evaluateJavaScript("document.getElementById('tool_open').click()");
        }
    }
    

    I can have all Webelements I want (have their name, attributes, etc.) but I can not interact with them, for example the click() function for my Webelement ("<li id="tool_open" >Open</li>") does not work whereas in Javascript it works...

    Any idea ?

    p3c0P Offline
    p3c0P Offline
    p3c0
    Moderators
    wrote on last edited by
    #2

    Hi @fgdevel,
    Some confusion here. Why are you using QWebEngineView and QWebView ? They both are different. The former uses Qt WebEngine while latter uses Qt Webkit. And Qt WebEngine lacks some API's. QWebElement is one of them.
    Can you reframe your example considering only QWebView as it only provides QWebElement ?

    157

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fgdevel
      wrote on last edited by
      #3

      Yes, but it does not work too.

      [ Qt ]
      webView = new QWebView;
      QVBoxLayout *layout = new QVBoxLayout;
      layout->addWidget(webView);
      [ ... ]
      QWebElement el = webView->page()->mainFrame()->findFirstElement("#tool_open");
      el.evaluateJavaScript("this.click()");

      the click() action doesn't work

      With an another Qt code, I can call a Javascript function, but all actions (click, mouse events...) don't work :
      [ Javascript ]
      function test()
      {
      alert('clic');
      document.getElementById('tool_open').click();
      }

      I have Js pop-up "clic" but the click() action on the webelement 'tool_source' doesn't work...

      p3c0P 1 Reply Last reply
      0
      • F fgdevel

        Yes, but it does not work too.

        [ Qt ]
        webView = new QWebView;
        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(webView);
        [ ... ]
        QWebElement el = webView->page()->mainFrame()->findFirstElement("#tool_open");
        el.evaluateJavaScript("this.click()");

        the click() action doesn't work

        With an another Qt code, I can call a Javascript function, but all actions (click, mouse events...) don't work :
        [ Javascript ]
        function test()
        {
        alert('clic');
        document.getElementById('tool_open').click();
        }

        I have Js pop-up "clic" but the click() action on the webelement 'tool_source' doesn't work...

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #4

        @fgdevel
        QWebElement el = webView->page()->mainFrame()->findFirstElement("#tool_open");

        Does this return the exact element that you wanted ?
        You can just do

        qDebug() << el.toPlainText(); //prints the button text
        

        If this works then evaluateJavaScript should work too.

        157

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fgdevel
          wrote on last edited by
          #5

          yes, already tried qDebug() << el.toPlainText();
          and it works...

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fgdevel
            wrote on last edited by
            #6

            oh no it doesn't work !

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fgdevel
              wrote on last edited by
              #7

              [ Qt ]
              QWebElement el = webView->page()->mainFrame()->findFirstElement("#tool_open");
              qDebug() << "WebElement : " + el.attribute("type");

              it works, el.plainText does not work for this kind of webelement

              el.evaluateJavaScript("this.click()");

              it does not work

              p3c0P 1 Reply Last reply
              0
              • F fgdevel

                [ Qt ]
                QWebElement el = webView->page()->mainFrame()->findFirstElement("#tool_open");
                qDebug() << "WebElement : " + el.attribute("type");

                it works, el.plainText does not work for this kind of webelement

                el.evaluateJavaScript("this.click()");

                it does not work

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #8

                @fgdevel

                el.plainText does not work for this kind of webelement

                It works too. It will print the list's text.
                Also for list click() works.
                What are you trying to do on click ? Can you post you HTML code which doesnot work ?

                157

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fgdevel
                  wrote on last edited by
                  #9

                  I try to simulate a webelement click thanks Qt.

                  In a web navigator the 'tool_open' click() opens an another web page (with javascript code...). I would like to do the same (simulate mouse clic) with Qt.

                  [ HTML ]
                  [ ... ]
                  <ul>
                  <li id="tool_clear">
                  <div></div>
                  Nouvelle Configuration
                  </li>
                  <li id="tool_open" >
                  <div id="fileinputs">
                  <div></div>
                  </div>
                  Open Image
                  </li>
                  </ul>
                  [ ... ]

                  p3c0P 1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    fgdevel
                    wrote on last edited by
                    #10

                    and yes the el.plainText works (I tried with an <input> HTML element instead of <li>...)

                    1 Reply Last reply
                    0
                    • F fgdevel

                      I try to simulate a webelement click thanks Qt.

                      In a web navigator the 'tool_open' click() opens an another web page (with javascript code...). I would like to do the same (simulate mouse clic) with Qt.

                      [ HTML ]
                      [ ... ]
                      <ul>
                      <li id="tool_clear">
                      <div></div>
                      Nouvelle Configuration
                      </li>
                      <li id="tool_open" >
                      <div id="fileinputs">
                      <div></div>
                      </div>
                      Open Image
                      </li>
                      </ul>
                      [ ... ]

                      p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by p3c0
                      #11

                      @fgdevel I dont see any code which gets invoked on click of tool_open. For eg . the following works i.e alert is called

                      <script>
                      function click(){
                                 alert("Clicked");
                      }
                      </script>
                      <li id="list" onclick="click()">Clickable list</li>
                      

                      and from Qt

                      QWebElement b = ui->webView->page()->mainFrame()->findFirstElement("#list");
                      b.evaluateJavaScript("this.click()");
                      

                      157

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        fgdevel
                        wrote on last edited by
                        #12

                        the action is realized by this function :
                        [ JS ]
                        var clickOpen = function() { [ ... ] }

                        but I don't know how to call this function in HTML, just clickOpen() doesn't work, even if I include the JS file in the HTML (<script src="myfile.js"></script>)

                        p3c0P 1 Reply Last reply
                        0
                        • F fgdevel

                          the action is realized by this function :
                          [ JS ]
                          var clickOpen = function() { [ ... ] }

                          but I don't know how to call this function in HTML, just clickOpen() doesn't work, even if I include the JS file in the HTML (<script src="myfile.js"></script>)

                          p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on last edited by
                          #13

                          @fgdevel Well then you need to first figure out how to call that function on click of li element in HTML.

                          157

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            maximus
                            wrote on last edited by
                            #14

                            evaluateJavaScript is your friend

                            Exemple of code working with Jquery, replace with javascript code

                                            QString jsToExecute += "$('#login-btn').click(); ";
                                            ui->webView_login->page()->mainFrame()->documentElement().evaluateJavaScript(jsToExecute + "; null" );
                            

                            Free Indoor Cycling Software - https://maximumtrainer.com

                            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