简体   繁体   中英

How to click a button with no id or name, inside an iframe?

I need to click the button (without an id and name) inside an iframe (with an id and name) with using JavaScript/jQuery.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript">
</script>
</head>
<body>
<iframe name="abb" id="abb" src="http://example.com" scrolling="no"frameborder="0" style="border:none; overflow:hidden; width:45px; left:-19px; height:21px; z-index: 0; position: relative;" allowTransparency="true"></iframe>
</div>

<script>
document.getElementById("abb").contentWindow.document.getElementsByName('REG_BTN')[0].click();
</script>
</body>
</html>

You can't access the DOM of documents on other origins.

The closest you could come would be to send a message to the document which would have to include JS that listened for the message and responded accordingly.

you could always traverse it by using id or name of the tag that you do know

for example

$('body div.container div.panel>div.heading~div button').click();

This is a way to locate the button:

var iframe = document.getElementById('iframeId'),
    innerDocument = iframe.contentDocument || iframe.contentWindow.document,
    button = innerDocument.getElementsByTageName('input')[0];

This will only work if the iframe origin from your server. This will find the first button, or <input> . If there's more than one <input> you could make a for loop and check for the 'button' type with getAttribute('button'); . This will help you doing the click simulation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM