[英]How to get a text in a div class inside an iframe with no id?
我正在尝试编写从以下html页面提取网站的tampermonkey脚本:
<html>
<head></head>
<body>
<iframe src="http://some-url.com/ll" title="test" name="ws_block" frameborder="0" scrolling="no" style="border:0px; width:100%; height: 320px;">
<html>
<head>
<!-- A particular character set can be specified by assigning "charset" -->
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Title</title>
<link rel="stylesheet" href="/en/Custom/blockStyle.css" type="text/css">
</head>
<body>
<div class="wrapper">
<div id="logo"><img src="/banner.gif" alt="[ORG]" border="0"></div>
<div class="block-body">
<div class="textLine">
<div class="ws-label label" data-strid="url"><script> ws.print("url"); </script>Address:</div>
<div class="text">https://www.my_website.com/</div>
</div>
<div class="textLine">
<div class="ws-label label" data-strid="category"><script> ws.print("category"); </script>Category:</div>
<div class="text">My-Website</div>
</div>
</div>
</div>
</body>
</html>
</iframe>
</body>
</html>
我正在尝试提取我的网站链接:
https://www.my_website.com/
window
对象具有一个包含所有页面中包含的iframe的frames
数组,因此您可以对其进行迭代并尝试获取其内容。
如果只有一个iframe,则window.frames[0].getElementsByClassName(classname)
可以解决问题。
请在MDN上查看其文档
出于安全原因,您无法通过javascript访问其他来源(域)iframe!
您应该通过像php这样的服务器端语言来做到这一点!
如果domain和iframe src位于一个来源,则可以使用r1verside的答案 :
window.frames[0].getElementsByClassName(classname)
在DOM中有很多不涉及使用ID的方法。
您需要找到元素的其他识别功能并加以使用。
例如:
document.querySelector("iframe");
…将返回文档中的第一个 iframe。
之后,您将使用与通过ID获得iframe时完全相同的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.