参考 Windows Powershell 中的弹出窗口

2024-04-07

我正在为我正在开发的网站进行测试自动化。我正在使用 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()

UPDATE:这是 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)

我非常感谢@sunliffe 帮助我解决这个问题。


此方法有一个拼写错误:$doc.getElementByID('link')

它应该是:

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

Update:

根据后续代码/评论...您应该能够使用如下内容提取对窗口的引用:

$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]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

参考 Windows Powershell 中的弹出窗口 的相关文章

随机推荐