如何在iframe中单击没有id或名称的按钮

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

本文关键字:id 按钮 iframe 单击      更新时间:2023-09-26

我需要使用JavaScript/jQuery在iframe(带有id和名称)中单击按钮(没有id和名称)。

<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>

不能访问其他源文件的DOM。

最接近的方法是向文档发送消息,该文档必须包含侦听消息并相应地响应的JS。

你可以通过使用id或者你知道的标签名来遍历它

例如

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

这是一种定位按钮的方法:

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

只有当iframe来自您的服务器时才会起作用。这将找到第一个按钮,或<input>。如果有一个以上的<input>,你可以做一个for循环,并检查与getAttribute('button');的"按钮"类型。这将帮助您进行点击模拟。