2

I am working on an iOS app that performs some actions in a webview using injected javascript. I am trying to press a submit button on a router's web-interface (So I can't give you a url). I've tried many other proposed solutions online but nothing is working. One thing I must do is click an input button of type=SUBMIT and for whatever reason it is not responding. I have tried using .click() along with calling the onClick event handler, but nothing is working. Any help is appreciated. The HTML code is below:

<div class="fix_button">
  <table width="100%" border="0" cellpadding="0" cellspacing="2">
    <tbody>
      <tr>
        <td nowrap="" colspan="2" align="center">
          <input class="cancel_bt" type="RESET" name="Cancel" value="Cancel" onclick="doCancel();" languagecode="51">
          &nbsp;
          <input class="apply_bt" type="SUBMIT" name="Apply" value="Apply" onclick="return checkData();" languagecode="50">
        </td>
      </tr>
    </tbody>
  </table>
</div>

The input button is the one with the class="apply_bt". This div is within an iframe and submits data that is within another iframe within this frame. The structure is sort of like this:

<iframe>
    // Contains the Apply Button
    <div class="fix_button"></div>

    // Contains the iframe where the info to be submitted is
    <div id="main" class="main_top_button">
        // Contains data to be submitted
        <iframe src="WLG_2g_wireless2.htm&amp;todo=wifi_primary_init" id="2g_setting" name="wl2gsetting" onload="SetCwinHeight(this.id)" marginheight="0" marginwidth="0" scrolling="no" width="100%" frameborder="0" height="288px">
        </iframe>
    </div>
</iframe>

This is the code I am using to click the button:

// Gets the outer iframe
iframe1 = document.getElementById("formframe");
// Gets the html within the iframe
var innerDoc=iframe1.contentDocument || iframe1.contentWindow.document;
// Gets iframe within the iframe
innerframe = innerDoc.getElementById("2g_setting");
// Gets the html within the inner iframe
var innerDoc2=innerframe.contentDocument || innerframe.contentWindow.document;

// I Enter some data in the inner iframe (not shown), this portion 
// of the code works perfectly. I can see fields being entered and 
// buttons being clicked.
// Then I click the apply button...
innerDoc.getElementsByName("Apply")[0].click()
// And nothing happens

I am able to get the element and read data from the Apply Button, such as its text, but I am not able to perform a .click() action on the button. Also, I am not getting any errors. I have no idea why its not working. Please help!

metacore
  • 91
  • 1
  • 9

2 Answers2

1

The click method is only reliable for <a> tags. You should fire a synthetic event instead. That will also guarantee that any event handlers are called, whether they were set as html attributes, element.onclick, addEventListener or any library you may be using.

See How can I trigger a JavaScript event click

Using the code from the above question, do the following.

var button = innerDoc.getElementsByName("Apply")[0] ;

fireEvent(button, 'click')
Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Thanks for the quick answer. I tried your code but unfortunately its not working. The button still does not get clicked. Still no errors showing up. The code before it that enters data into the inner frame works great, but the event still does not fire. Any idea why? Are there any other solutions? – metacore Jul 13 '16 at 23:20
0

The problem is that the script is inside of another iFrame, and so it is not able to grab it if you are not inside of that iFrame. Therefore, the onclick event will fail. You must get the functions from the inner Iframe first in order to run them from the outer iFrame.

Ian Richard
  • 525
  • 4
  • 9