Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I interact with an Iframe using Powershell?

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?

like image 471
not-bob Avatar asked Oct 28 '25 20:10

not-bob


1 Answers

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

like image 94
spanda Avatar answered Oct 31 '25 11:10

spanda