html - 如何使用PowerShell在新窗口中引用框架中的内容?

标签 html windows internet-explorer powershell frames

我正在使用 PowerShell 登录 Web 应用程序。登录后,应用程序将打开一个新窗口。新窗口有一个包含三个框架的框架集。我正在尝试单击其中一个框架(“左”)中的链接。

$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.navigate("http://example.local")
while($ie.ReadyState -ne 4) { Start-Sleep -Seconds 5 }
$ie.visible = $true
$doc = $ie.document

$userField = $doc.getElementById("uid")
$passwordField = $doc.getElementById("pwd")
$submitButton = $doc.getElementById("submit")

$userField.value = "userid"
$passwordField.value = "*****"
$submitButton.click()

$shell = New-object -ComObject "Shell.Application"
# The app opens a new window with a timestamp - that's the window I want, so I skip the other window
$ie2 = $shell.Windows() | where {$_.Type -eq "HTML Document" -and $_.LocationName -ne "My App - Parent Window"}
$doc2 = $ie2.document
$frames = @($doc2.getElementsByTagName("FRAME"))
Write-Host $frames.Count # shows "3"
Write-Host $frames[1].Title # shows "Left Nav"

如何引用左框架?我尝试了各种我认为可行的组合,主要是 $frames[1].document.something... 的一些变体。

HTML(不是我的代码):

<frameset rows="60,*">
    <frame src="http://example.local/really/long?url" title="Top Nav" name="Top" id="Top">
    <frameset cols="175,*">
        <frame src="http://example.local/left.html" title="Left Nav" name="Left" id="Left">
        <frame src="http://example.local/main.html" title="Main Display" name="Main" id="Main">
    </frameset>
</frameset>

我没有使用 $frames 数组,而是按照@Adi的建议使用了frames属性。

$frames = $doc2.frames
$left = $frames.item([ref]1).document      # the frame I need
$left_frames = $left.frames                # this page has frames too
$nav = $left_frames.item([ref]0).document  # the frame I need
$nav.body.outerHTML                        # gives me the code I need

最佳答案

不要通过调用 Document 对象的 GetElementsByTagName() 方法将帧读入元素数组,而是使用 Document 对象的 frames 属性,该属性将返回一个专门的对象用于与框架交互:

$frames = $ie2.document.frames

这将返回一个代表所有帧的对象。要引用各个帧,$frames[1]不起作用,因为 $frames 不是一个集合,因此您无法对其进行索引。它是一个 ComObject,表示同一类型项目的分组的 ComObject 通常具有一个 Item 方法,用于通过指定索引号作为参数来引用该组中的各个项目。使用$frames | gm检查可用的属性和方法,您可以看到实际上有一个 item 方法。

我不确定索引号是如何分配给帧的,但大概是某种从左到右,然后从上到下的方案,所以如果你有一个顶部、左侧和右侧的框架,可以肯定索引号将为 1。试试这个:

$frames.item(1).document.something...

关于html - 如何使用PowerShell在新窗口中引用框架中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24896180/

相关文章:

jQuery - 检测 <video> onEnded

javascript - IE 开发者工具断点不起作用

html - 防止文本输入在 IE 中扩展

c# - 关系型 SQL Server 数据库

html - 如何在 HTML 文本输入字段中隐藏插入符号?

windows - 在 Windows 中批量重命名子目录

ruby - 是否有任何用于 Windows 和 Unix 的远程脚本编写的框架?

windows - 使用其他语言版本创建 Azure Windows VM

css - 使用 CSS 在 IE 中显示已经隐藏的表格单元格

jquery - html中的固定按钮