简体   繁体   中英

How to get a text in a div class inside an iframe with no id?

I am trying to write a tampermonkey script that extract a website from the following html page:

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

I am trying to extract my website link:
https://www.my_website.com/

The window object has a frames array with all the iframes contained in your page, so you can iterate it and try to get your content.

If you only have an iframe then window.frames[0].getElementsByClassName(classname) will do the trick.

Please have a look at its documentation at MDN

You cannot access into other origin (domain) iframes by javascript for security reasons!

You should do it by server side languages like php!

if the domain and iframes src are in one origin you can use like r1verside's answer :

window.frames[0].getElementsByClassName(classname)

There are lots of different methods to find an element in the DOM that don't involve using their ID.

You need to find some other identifying feature of the element and use that.

For example:

document.querySelector("iframe");

… will return the first iframe in the document.


After that, you would use exactly the same method as you would use if you had got the iframe by the id.

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