0

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>
8
  • elmnt.click; is not clicking the element. You are not executing it. Commented May 17, 2019 at 19:46
  • elmnt.click; should be elmnt.click(); otherwise you're just returning the click method and not executing it: developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click Commented May 17, 2019 at 19:46
  • sorry for the typo! But it still not working... Commented May 17, 2019 at 19:49
  • Only way you will be able to click on an item in the iframe is if it is in the same domain. Commented May 17, 2019 at 19:49
  • @epascarello, I am not sure what you mean, but my file is a local file, so does it mean, that I have no solution to send a click on this button ? Commented May 17, 2019 at 19:55

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.