internet-explorer - 对Windows Powershell中弹出窗口的引用

标签 internet-explorer powershell

我正在为我正在工作的网站进行测试自动化。我正在使用Windows Powershell创建脚本来执行此操作。我的问题是我需要单击打开另一个窗口的链接,我需要对该窗口的一些引用。

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

那是我对浏览器和链接的引用。然后,我单击链接:
$link.click()

这会打开一个新窗口,其中包含我需要测试的内容。我如何获得对这个新窗口的引用?从技术上讲,它不是第一个的子窗口。

我试图将链接的点击设置为像这样的引用,但是它不起作用:
$test = new-object -com "InternetExplorer.Application"
$test = $link.click()

更新:
这是调用打开窗口的JavaScript函数openwindow
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();
   };
};

在看起来像这样的代码行中调用该函数
column.For(i => "<a href='" + link + i.id + "?status=new' target='pat" + i.id + "'id'enc' onclick='openwindow(this.href,this.target);return false'>

最终更新:
我最终做了些微的改变。我创建了一个新的Internet Explorer对象,并从链接中获取了href并设置了所有选项,并像javascript一样使用powershell导航到了窗口。
$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)

我非常感谢@scunliffe为我解决这个问题。

最佳答案

此方法中有一个错字:$ doc.getElementByID('link')

它应该是:

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

更新:

根据后续代码/注释,您应该能够使用以下内容提取对窗口的引用:
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://localhost:4611")
$ie.visible = $true
$doc = $ie.document
$link = $doc.getElementById('link')
$link.click()

链接具有一个目标属性集,openwindow函数使用该属性为弹出窗口分配名称。

openwindow函数本身将对弹出窗口的引用存储在名为winrefs的属性中,因此现在应该可以获取该窗口...(如果我对IE窗口中PowerShell语法的期望正确的话)。
$targetName = $link.target
$popupHandle = $ie.openwindow.winrefs[$targetName]

关于internet-explorer - 对Windows Powershell中弹出窗口的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5434586/

相关文章:

javascript - IE 无法正确循环 HtmlCollection

javascript - 在 IE 中打开时 Iframe 内的导航问题

css - IE 10 不支持纯文本装饰

powershell - 按值对嵌套哈希表排序

regex - 正则表达式-在Powershell中替换

internet-explorer - 溢出 :hidden; strange behavior at Internet Explorer on mouse over

javascript - Google Graphs IE11 appendChild 不工作

javascript - 如何在 PowerShell 中捕获闭包编译器输出?

powershell - 是否可以在 powershell 3.0 的 Invoke-WebRequest 中使用 document.querySelector?

powershell - 您的 token 尚未被授予执行此查询所需的范围。 'id' 字段需要以下范围之一 : ['read:packages' ],