gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:04:56 33 4
gpt4 key购买 nike

我正在为我正在工作的网站进行测试自动化。我正在使用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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com