0

I'm pretty sure this is impossible, but I need to make sure before I give up and go another route.

I have a page that is within an iframe and the menu is outside of it. I need to programmatically click on one of those menu items to make an action happens. The application is built weird so I'm trying to work with it and just make it work. I cannot make any html changes.

My html looks something like this.

<body>
  <header>
    <p><a id='menuItem'>Menu Item</a></p>
    <p><a id='anotherMenuItem'>Another Menu Item</a></p>
  </header>
  <div id='mainContent'>
    <iframe>
     //Where all the content is and where the click will be happening.
    </iframe>
  </div>
</body>

and this is the click that needs to happen

$("#menuItem").click()

I've tried traversing to escape the iframe, but I have had no luck. I have also just tried calling the click, but there is no context so it has no idea what it's doing.

i have also tried this Access elements of parent window from iframe

But because of how this grabs elements it does not work with the click() function. Or at least when I try I cannot get it to work

window.parent.document.getElementById('#menuItem').click()

I cannot even get a window.parent.document.getElementById('#menuItem').length to find the element.

zazvorniki
  • 3,512
  • 21
  • 74
  • 122
  • why you dont set a transparent div inside the iframe then trigger a click on that div ? – Abslen Char Apr 09 '18 at 13:55
  • 1
    @AbdeslemCharif, because I cannot change the html. Plus inside the iframe it has no context of anything outside of it. – zazvorniki Apr 09 '18 at 13:56
  • Possible duplicate of [Access elements of parent window from iframe](https://stackoverflow.com/questions/7027799/access-elements-of-parent-window-from-iframe) – CBroe Apr 09 '18 at 13:59
  • @CBroe, I've already looked at that. The click didn't work with that because of how that spits out the html. – zazvorniki Apr 09 '18 at 14:00
  • @zazvorniki please update the questions to include that into the "what have you tried" section – Mauricio Gracia Gutierrez Apr 09 '18 at 14:02
  • _“The click didn't work with that because of how that spits out the html”_ - I have no idea what you mean by that, or how/why it should be an explanation. Please provide a proper [mcve] of what you tried. – CBroe Apr 09 '18 at 14:02
  • Do you actually mean that this “menu” doesn’t contain any actual, proper links itself either (jeez, who builds sh_t like that?), but only triggers JS functionality via event handlers on those href-less links? (Then we’d still need more details to know what the situation actually is.) – CBroe Apr 09 '18 at 14:05
  • Do the top frame and the iframe have [the same origin](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy)? – Kos Apr 09 '18 at 14:08
  • Calling the _jquery_ `click` method is something different, than calling a `click` method of an HTML element object. – CBroe Apr 09 '18 at 14:09
  • @CBroe, the html looks like what I posted above. They do not go to pages, they perform actions. This one changes the iframe and then drops down a search. – zazvorniki Apr 09 '18 at 14:10
  • @Kos, yes they do have the same origin. – zazvorniki Apr 09 '18 at 14:10

1 Answers1

0

This code looks fishy:

window.parent.document.getElementById('#menuItem').click()

The # is part of selector syntax, not part of the ID itself. So it's either:

window.parent.document.getElementById('menuItem').click()

or:

window.parent.document.querySelector('#menuItem').click()
Kos
  • 70,399
  • 25
  • 169
  • 233
  • If this doesn't help, please let me know what happens when you try it. Trying the code manually in browser devtools looks like a good idea to start debugging. – Kos Apr 09 '18 at 14:14
  • I have tried all of the above. I only debug in my console. :) They all return Uncaught TypeError: Cannot read property 'click' of null They cannot find the element. – zazvorniki Apr 09 '18 at 14:15
  • Sounds like `window.parent.document` is not the document in question. Can you prepare a sample that I can open in my browser? – Kos Apr 09 '18 at 14:24
  • @zazvorniki _“I only debug in my console”_ - and you have made sure that in this case the “context” is actually that of your inner frame? – CBroe Apr 09 '18 at 14:33