In QWebKit , how can I clicked the web html's button by program?



  • a id="mobileRegA" href="[removed]void(0);" onclick="_Global.mobile.start();return false;" class="btnReg">
    Register

    [removed]void(0) ==》 avascript:void(0);
    here's code snippet ,
    @
    QWebFrame *frame = ui.webView->page()->mainFrame();
    QWebElement mainRegA = frame->findFirstElement("#mainRegA");
    mainRegA.evaluateJavaScript("this.clicked");
    @

    what's wrong? thanx



  • Just use ...evaluateJavaScript("document.getElementById("mainRegA").clicked()

    Also you are searching for #mainRegA (name includes #).


  • Moderators

    taken from the QWebElement docs:
    @
    QWebElement document = frame->documentElement();
    /* Assume that the document has the following structure:

         <form name="myform" action="submit_form.asp" method="get">
             <input type="text" name="myfield">
             <input type="submit" value="Submit">
         </form>
    
      */
    
     QWebElement button = document.findFirst("input[type=submit]");
     button.evaluateJavaScript("this.click()");
    

    @

    Compare this with your code: the problem is that a-Elements don't support the click() method (only input-Elements).
    Are you using jQuery? AFAIK jQuery would provide an click() method for a-Elements.
    If not you need to do something like this:
    @
    var href = document.getElementById("mainRegA").getAttribute('href');
    [removed].href = href;
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.