Cheap VPS host selection
Provide server host evaluation information

How to locate elements when a frame exists on a web page

To locate an element in a web page containing a frame, you can use the following methods:

1. Via window.frames Property gets the frame object:

 var frame = window . frames [ "frameName" ];

there "frameName" It is the name or index of the frame element (such as 0, 1, etc.). adopt window.frames Property can access all frame objects in the current window.

2. After the frame is loaded document Attributes and DOM methods to locate elements:

 frame. onload = function () { var element = frame. document . getElementById ( "elementId" ); //Operate on the elements in the frame };

Here we use onload Event handler to ensure that the code is executed after the frame document is loaded. Then, by getElementById Or other DOM methods locate specific elements on the frame's document object.

It should be noted that in the case of cross domain, the elements in the frame cannot be directly accessed due to the restriction of the same origin policy. If the frame and the parent page do not belong to the same domain name, protocol, and port, a security error will occur. In this case, the postMessage method can be used for cross domain communication to obtain the elements in the frame.

In addition, to ensure that the elements can be located after the frame is loaded onload Event, or use the setTimeout To ensure that the elements of the frame have been loaded.

Do not reprint without permission: Cheap VPS evaluation » How to locate elements when a frame exists on a web page