在Windows Powershell中引用弹出窗口 [英] Reference to Pop-up window in Windows Powershell
问题描述
我正在为我正在研究的网站进行测试自动化。我使用Windows PowerShell来创建脚本来执行此操作。我的问题是我需要单击一个打开另一个窗口的链接,我需要一个如何引用该窗口。
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()
更新:
这是调用打开窗口的JavaScript函数openwindow
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'>
最终更新:
我的结果略有不同。我创建了一个新的Internet Explorer对象并从链接中抓取了href并设置了所有选项并使用像javascript一样的powershell导航到窗口。
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)
我非常感谢@scunliffe帮助我解决这个问题。
I'd like to thank @scunliffe so much for helping me through this problem.
推荐答案
此方法中存在拼写错误:$ doc.getElementByID('link')
there's a typo in this method: $doc.getElementByID('link')
它应该是:
$doc.getElementById('link')
^ lowercase d
更新:
根据跟进代码/评论...您应该能够使用以下内容提取对窗口的引用:
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()
该链接具有一个目标属性集,openwindow函数使用该属性为弹出窗口指定名称。
The link has a target attribute set that the openwindow function uses to assign a name to the popup window.
openwindow函数本身存储对弹出窗口的引用在一个名为winrefs的属性中,因此现在应该得到窗口...(如果我对PowerShell sy的期望IE窗口中的ntax是正确的。
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]
这篇关于在Windows Powershell中引用弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!