MagTun
MagTun

Reputation: 6185

Click on a button in a iframe vanilla js

I would like to load an ifram and then click on a button inside this iframe (the button is the on with a little umbrella on the left just above the forecats for the next 14 days)

So far this is what I was able to do, but it's not working:

<style>
  #my-div {
    width: 80%;
    height: 1500px;
    overflow: hidden;
    position: relative;
  }

  #my-iframe {
    position: absolute;
    top: -480px;
    left: -100px;
    width: 1280px;
    height: 1400px;
  }
</style>

<div id="my-div">
  <iframe src="http://www.meteofrance.com/previsions-meteo-france/paris-01e-arrondissement/75001" id="my-iframe"
    scrolling="no"></iframe>
</div>


<script>
  var iframe = document.getElementById("my-iframe");

  iframe.addEventListener("load", function () {
    console.log(iframe)
    var elmnt = iframe.contentWindow.document.getElementById("mf-accordion-bandeau-btn-unfold");
    console.log(elmnt)
    elmnt.click();
  });
</script>

Upvotes: 0

Views: 728

Answers (1)

dejakob
dejakob

Reputation: 2092

Accessing the contents of an iframe with JavaScript is only possible in certain cases. The origin and protocol must be the same and also there can't be any headers on the child preventing the main page to access it.

This is pure for security reasons, as otherwise, websites would just be able to load 3rd party sites and steal personal data or act as the user.

Upvotes: 1

Related Questions