Reference to Pop-up window in Windows Powershell

2019-07-14 04:36发布

I am working on testing-automation for a website I'm working on. I am using windows powershell to create scripts to do this. My problem is I need to click a link that opens up another window, I need a reference to that window some how.

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://localhost:4611")
$ie.visible = $true
$doc = $ie.document
$link = $doc.getElementByID('link')

That is where I get a reference to the browser and the link. Then I click the link:

$link.click()

Which opens up a new window with things I need to test on it. How would I get a reference to this new window? It is not technically a child window of the first one.

I have tried to set the click of the link to a reference like this but it doesn't work:

$test = new-object -com "InternetExplorer.Application"
$test = $link.click()

UPDATE: Here is the JavaScript function openwindow that gets called to open the window

function openwindow(url, name) {
   if (typeof openwindow.winrefs == 'undefined') {
      openwindow.winrefs = {};
   }
   if (typeof openwindow.winrefs[name] == 'undefined' || openwindow.winrefs[name].closed) {
    openwindow.winrefs[name] = window.open(url, name, 'scrollbars=yes,menubar=no,height=515,width=700,resizable=no,toolbar=no,status=no');
   } else {
      openwindow.winrefs[name].focus();
   };
};

The function gets called in a line of code that looks like this

column.For(i => "<a href='" + link + i.id + "?status=new' target='pat" + i.id + "'id'enc' onclick='openwindow(this.href,this.target);return false'>

FINAL UPDATE: I ended up doing this slightly differently. I created a new Internet Explorer object and grabbed the href from the link and set all of the options and navigated to the window using powershell like the javascript does.

$ie2 = new-object -com "InternetExplorer"
$ie2.visible = $true
$ie2.menubar = $false
$ie2.height = 515
$ie2.width = 700
$ie2.resizable = $false
$link = $doc.getelementbyid('link')
$url = $link.href
$ie2.navigate($url)

I'd like to thank @scunliffe so much for helping me through this problem.

1条回答
我只想做你的唯一
2楼-- · 2019-07-14 05:06

there's a typo in this method: $doc.getElementByID('link')

it should be:

$doc.getElementById('link')
                  ^ lowercase d

Update:

Based on the follow up code/comments... you should be able to extract a reference to the window with something like this:

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://localhost:4611")
$ie.visible = $true
$doc = $ie.document
$link = $doc.getElementById('link')
$link.click()

The link has a target attribute set that the openwindow function uses to assign a name to the popup window.

The openwindow function itself, stores a reference to the popup in a property called winrefs, thus this should now get the window... (if my expectations of the PowerShell syntax within the IE window are correct.

$targetName = $link.target
$popupHandle = $ie.openwindow.winrefs[$targetName]
查看更多
登录 后发表回答