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?
Below code worked for me, some of info got from http://www.dyn-web.com/tutorials/iframes/
# give your url here in this line instead of sample url
& "$env:programfiles\Internet Explorer\iexplore.exe" 'http://blahblahblah'
$win = New-Object -comObject Shell.Application
$try = 0
$ieObj = $null
do {
Start-Sleep -milliseconds 500
# plese use your title instead of "your_title" to identify the window correct
$ieObj = @($win.windows() | ? { $_.locationName -like '*your_title*' })[0]
$try ++
if ($try -gt 20) {
Throw "Web Page cannot be opened."
}
} while ($ieObj -eq $null)
[System.Threading.Thread]::Sleep(1000)
# put both Iframe name and id both to "fraMain"
$ieObj.document.getElementbyID("fraMain").contentWindow.document.getElementbyID("name").value = "test name"
$ieObj.document.getElementbyID("fraMain").contentWindow.document.getElementbyID("button").Click()
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With