2013-01-23 64 views
1

我們正在嘗試使用Powershell在我們的網站上自動執行一些測試。 當我們在打開新選項卡的定位點上發出點擊事件時,我們遇到了問題。 我們需要在新選項卡上獲得句柄,然後通過單擊新選項卡上的打印按鈕繼續處理。 這怎麼可以使用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() 

我們是Powershell的新手,所以任何幫助,將不勝感激。

感謝 理查德

+0

你能分享你的代碼嗎? – David

+0

不容易在沒有看到您的代碼的情況下回答您的問題。不過,您可以在這裏找到一些曲目:http://msdn.microsoft.com/en-us/magazine/cc163301.aspx –

回答

1

嘗試使用$ ie.navigate2爲0x0800把它放在一個新的前臺選項卡中打開新的標籤。這會自動將您聚焦在新選項卡上,讓您抓住打印按鈕。

例如

# 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)