简体   繁体   English

Powershell在新窗口上获取句柄

[英]powershell get handle on new window

We are trying to use Powershell to automate some testing on our site. 我们正在尝试使用Powershell在我们的网站上自动化一些测试。 We have hit a problem when we issue the click event on an anchor which opens a new tab. 当我们在锚点上发布click事件时遇到问题,该锚点会打开一个新标签页。 We need to get a handle on the new tab and then continue processing by clicking the print button on the new tab. 我们需要在新选项卡上找到一个句柄,然后通过单击新选项卡上的打印按钮来继续进行处理。 How could this be done using Powershell? 如何使用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()

We are new to Powershell so any help would be appreciated. 我们是Powershell的新手,任何帮助将不胜感激。

Thanks Richard 谢谢理查德

Try opening your new tab using $ie.navigate2 and 0x0800 to put it in a new foreground tab. 尝试使用$ ie.navigate20x0800打开新标签,将其放在新的前景标签中。 That will automatically focus you on the new tab allowing you to grab the print button. 这将自动将您聚焦在新选项卡上,使您可以抓住打印按钮。

eg 例如

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

Similarly, you can use 0x1000 to open the page in a new background tab, which will open the new tab but not automatically bring it to focus. 同样,您可以使用0x1000在新的背景标签中打开页面,这将打开新标签,但不会自动使其聚焦。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM