Jump to content

How to get access to elements embedded in a iframe?


jwei
 Share

Recommended Posts

I have a frame, and inside it, there's an iframe.

Anyone can tell me how to gain access to elements within the iframe? Great thanks!

Following code snippet shows the methods I tried (and failed):

; Get frame objects of the OUTER frame and the INNER iframe.
$oMainFrame = _IEFrameGetObjByName($oIE, "main_frame")
$oSubIFrame = _IEGetObjById($oMainFrame, "sub_iframe_1")

; Try to get element within the OUTER frame.
; And it seems that both the following methods will work for getting the element object.
$oElem1 = $oMainFrame.document.getElementById("main_frame_elem_1")
$oElem2 = $oSubIFrame.document.getElementById("main_frame_elem_1")
ConsoleWrite("obj info: [" & ObjName($oElem1) & "]" & @CRLF)
ConsoleWrite("obj info: [" & ObjName($oElem2) & "]" & @CRLF)

; Try to get element within the INNER iframe.
; Unfortunately both the following methods will fail to get the element object :(
$oElem3 = $oMainFrame.document.getElementById("sub_iframe_elem_1")
$oElem4 = $oSubIFrame.document.getElementById("sub_iframe_elem_1")
ConsoleWrite("obj info: [" & ObjName($oElem3) & "]" & @CRLF)
ConsoleWrite("obj info: [" & ObjName($oElem4) & "]" & @CRLF)

The obj info for $oElem1 and $oElem2 are both right.

While the obj info for $oElem3 and $oElem4 are empty.

Expecting your reply...

Link to comment
Share on other sites

What is output to the SciTe console?

Why are you mixing the calls to _IE functions with low level DOM references that do the same thing? The _IE functions will give you error handling and helpful warning messages.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

What is output to the SciTe console?

Why are you mixing the calls to _IE functions with low level DOM references that do the same thing? The _IE functions will give you error handling and helpful warning messages.

Dale

Thanks for your reminder for not mixing _IE with low level DOM reference.

For the above code snippet, SciTe console output is like follows:

obj info: [DispHTMLTable]

obj info: [DispHTMLTable]

obj info: []

obj info: []

So the last two statements both failed. And if I replace them with _IEGetObjById, I indeed got warning messages like "IE.au3 Warning from function _IEGetObjById, $_IEStatus_NoMatch". While there should be a match as I expected.

I'm new to web auto test, and very fond of AutoIt. I'm trying to get familiar with IE.au3 so that I could make it more helpful for my routine tasks.

Expecting more suggestions on this issue and beyond, many many thanks indeed!

Link to comment
Share on other sites

What is output to the SciTe console?

Why are you mixing the calls to _IE functions with low level DOM references that do the same thing? The _IE functions will give you error handling and helpful warning messages.

Dale

Dale,

I read through the source code of IE.au3, and found one thing I'm not sure, it's about function parameters' description:

For some functions such as _IEFormGetCollection, the param $o_object can be iFrame.

;===============================================================================
;
; Function Name:    _IEFormGetCollection()
; Description:      Returns a collection object variable representing the Forms in the document
; Parameter(s):     $o_object   - Object variable of an InternetExplorer.Application, Window, Frame or iFrame object

While for other functions like _IEGetObjById, the $o_object is not claimed to be able to an iFrame.

;===============================================================================
;
; Function Name:    _IEGetObjById()
; Description:      Returns an object variable by Id or name
; Parameter(s):     $o_object   - Object variable of an InternetExplorer.Application, Window or Frame object

So far I have only very basic ideas about DOM, I'm not sure the difference between the management of Frame and iFrame.

I wonder if _IEGetObjById can work with an iFrame. Can I offer an iFrame object where a Frame object is expected, and hope the function will actually work with my iFrame object?

Thanks!

Link to comment
Share on other sites

Frame and iFrame can be interchanchangably in this context.

There is something else that is an important nuance however. When you get a reference to a Frame or iFrame, you can get either a "Frame as tag" or a "Frame as window".

A "Frame as tag" (which you would get from GetElementById or _IEGetObjById) is typically not what you want -- with it you can only get attribute information about the Frame or iFrame tag itself (like src), but cannot access its contents (a document).

It is a "Frame as window" that can be used in the places you can use the InternetExplorer object because it is a document container and is the parent of the document it contains. _IEFrameGetObjByName and _IEFrameGetCollection return this sort of object. You'll see that ObjName() for both Frame and iFrame gotten this way will return the same object type.

Work through the examples for _IEFrame* to get a feel for this.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Frame and iFrame can be interchanchangably in this context.

There is something else that is an important nuance however. When you get a reference to a Frame or iFrame, you can get either a "Frame as tag" or a "Frame as window".

A "Frame as tag" (which you would get from GetElementById or _IEGetObjById) is typically not what you want -- with it you can only get attribute information about the Frame or iFrame tag itself (like src), but cannot access its contents (a document).

It is a "Frame as window" that can be used in the places you can use the InternetExplorer object because it is a document container and is the parent of the document it contains. _IEFrameGetObjByName and _IEFrameGetCollection return this sort of object. You'll see that ObjName() for both Frame and iFrame gotten this way will return the same object type.

Work through the examples for _IEFrame* to get a feel for this.

Dale

Thanks!

Now it works by using _IEFrameGetCollection instead. That's great!

$oSubIFrame = _IEFrameGetCollection($oMainFrame, $i_Index)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...