如何使用Powershell与Iframe交互?

如何使用Powershell与Iframe交互?,iframe,powershell,automated-tests,testing,Iframe,Powershell,Automated Tests,Testing,我正在尝试自动化一个网站,并发现自己需要访问iframe的内容。因为这是一个内部应用程序,所以我加入了这个示例,它说明了错误 $ie = new-object -com "InternetExplorer.Application" $ie.navigate("http://arstechnica.com/") $ie.visible = $true $doc = $ie.document $maglistcontrol = $doc.getElementById("mag_list")

我正在尝试自动化一个网站,并发现自己需要访问iframe的内容。因为这是一个内部应用程序,所以我加入了这个示例,它说明了错误

$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"
这是我收到的错误消息

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
不能对空值表达式调用方法。
第6行字符:38

+$maglistcontrol=$doc.getElementById我尝试过这样做,但未能成功地从PowerShell操作IE(奇怪;我得到了一个$IE变量,但此后所有访问都失败了)

框架位于窗口对象中:框架集合

理论上,您可以通过$ie.document.parentWindow获取窗口对象

因此,这应该是可行的,但我无法测试它:

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

希望这有帮助。

您的问题似乎是COM对象变得不可用。我见过这样的情况,有时当一个新的IE对象被用来处理请求时(我不知道为什么,也许有人知道)。您需要再次找到窗口,如下所示:

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

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

但是我找不到你要找的“mag_list”标签。

下面的代码对我有用,一些信息来自


希望这有帮助

$w.frames不会返回任何值,这意味着$w.frames[0]也没有任何值是的,我不明白。窗口对象应该具有frames属性。请参阅示例。看起来powershell没有授予对此模型的完全访问权限。。。。(叹气)。也许是其他方式?但是如果我可以在页面上看到iframe以外的其他元素,那么我的COM对象怎么会不可用呢?根据您的错误消息,document元素是$null。由于这是我在你的帖子中看到的唯一信息,我根据我以前看到的情况提出了一个可能的解决方案。我自己尝试了你的代码,访问成员没有任何问题。但我找不到您所引用的iframe。当iframe引用同一网站中的页面信息时,这对我来说非常有效。然而,当我从一个完全不同的网页获取一个iFrame时,我遇到了一个问题。contentWindow.document对于该文档为空。你知道为什么吗?
# 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()