powershell得到新窗口的句柄

时间:2013-01-23 00:16:27

标签: powershell

我们正在尝试使用Powershell在我们的网站上自动执行某些测试。 当我们在打开新标签的锚点上发出click事件时,我们遇到了问题。 我们需要处理新选项卡,然后单击新选项卡上的打印按钮继续处理。 怎么能用Powershell做到这一点?

set-executionpolicy remotesigned
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("xxxxx.com")
$ie.visible = $true
$doc = $ie.document
While ($ie.Busy) {Sleep 10}
$doc -eq $null
$tb1 = $doc.getElementByID("username")
$tb2 = $doc.getElementByID("password")
$tb1.value = "xxxxxxxxxxx"
$tb2.value = "xxxxxxxxxxx"
$btn =  $ie.document.getelementsbytagname("input")|where{$_.name -eq "Submit"} 
$btn.click()
$ie.navigate("xxxxx.com/admin/reports.html#")
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -eq 'All Photos'}
$link.click()

<<<<<<<<<<< it's here where a new tab is open and we need to get a handle on the new page to be able to click the print button >>>>>>>>>>>

$popupHandle = $ie.openwindow.winrefs[$targetName]
$btn =  $popupHandle.document.getelementsbytagname("input")|where{$_.name -eq "print"}
$btn.click()

我们是Powershell的新手,所以任何帮助都会受到赞赏。

由于 理查德

1 个答案:

答案 0 :(得分:1)

尝试使用 $ ie.navigate2 0x0800 打开新标签,将其置于新的前景标签中。这将自动将您关注到新选项卡,允许您抓取打印按钮。

e.g。

# Setting page to new foreground tab here
$newTabPage = $ie.navigate2("www.xxxx.com",0x0800)

同样,您可以使用 0x1000 在新的背景标签页中打开该页面,该标签会打开新标签页,但不会自动将其置于焦点位置。

# Setting page to new background tab here
$newTabPage = $ie.navigate2("www.xxxx.com",0x1000)