views:

41

answers:

2

I'm trying to automate a website and find myself needing to get to the contents of an iframe. Since this is an internal application, I've put in this sample, which illustrates the error

$ie = new-object -com "InternetExplorer.Application" 
$ie.navigate("http://arstechnica.com/") 

$ie.visible = $true 
$doc = $ie.document 
$maglistcontrol = $doc.getElementById("mag_list") 
$maglistcontrol.value= "Concierge"

Here is the Error message I get

You cannot call a method on a null-valued expression.
At line:6 char:38
+ $maglistcontrol = $doc.getElementById <<<< ("mag_list") 
    + CategoryInfo          : InvalidOperation: (getElementById:String) [], RuntimeExce 
   ption
    + FullyQualifiedErrorId : InvokeMethodOnNull

Property 'value' cannot be found on this object; make sure it exists and is settable.
At line:7 char:17
+ $maglistcontrol. <<<< value= "Concierge"
    + CategoryInfo          : InvalidOperation: (value:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

The problem is, the mag_list field is in an iframe and the reference is not valid. Any ideas?

A: 

I tried to do this but could not succeed in manipulating IE from PowerShell (weird; I got a $ie variable, but all accesses thereafter fail).

Frames are in the Window object: the frames collection.

Theoritically, you can get at the window object through $ie.document.parentWindow.

So, this should work, but I could not test it:

$doc = $ie.document
$w = $doc.parentWindow
$fr = $w.frames[0] # assuming you want the first frame
$uidfield = $fr.document.getElementById("uid")

Hope this helps.

Timores
$w.frames doesn't return any values futher meaning that $w.frames[0] has no value either
not-bob
Yeah, I don't understand. The window object is supposed to have a frames property. See http://www.w3schools.com/jsref/obj_window.asp for example.
Timores
looks like powershell doesn't give full access to this model.... (sigh). Maybe some other way?
not-bob
A: 

It looks like your problem is the COM object becoming unusable. I have seen this happen sometimes when a new IE object is spawned to handle the request (I'm not sure why, maybe someone knows). You will need to find the window again like this:

$Shell = New-Object -COM Shell.Application
$Shell.Windows()  ## Find the right one in the list

$ie = $Shell.Windows().Item(1)  ## Grab the window

But I wasn't able to find that "mag_list" tag you were looking for.

JasonMArcher
But if I can see other elements on the page other than the iframe, how is it that my COM object is unusable?
not-bob
Well, according to your error message, the document element is $null. With that being the only information I see in your post, I suggested a possible solution based on what I have seen before. I tried your code myself and didn't have any problems accessing the members. But I couldn't find that iframe you were refering to.
JasonMArcher