【问题标题】:Reference to Pop-up window in Windows Powershell参考 Windows Powershell 中的弹出窗口
【发布时间】:2011-03-25 15:29:34
【问题描述】:

我正在为我正在开发的网站进行测试自动化。我正在使用 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 并设置所有选项并使用 powershell 导航到窗口,就像 javascript 一样。

$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 帮助我解决了这个问题。

【问题讨论】:

    标签: internet-explorer powershell


    【解决方案1】:

    这个方法有个错字:$doc.getElementByID('link')

    应该是:

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

    更新:

    根据后续代码/cmets...您应该能够通过以下方式提取对窗口的引用:

    $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]
    

    【讨论】:

    • windows powershell 不区分大小写
    • 啊,我很抱歉...我只是习惯于在网络上看到这种旅行。那么为什么这个链接会打开一个新窗口呢?它有目标属性集吗?还是调用 JavaScript?
    • 啊,好的,在这种情况下,跟踪 JavaScript……如果是的话……foo = window.open(...); 如果它提交表单或“点击”另一个带有target 的链接,您应该可以执行$ie.targetName(如果我正确理解 PowerShell 如何进入 IE)。
    • javascript函数不是我写的。 openwindow.winrefs[name] = window.open(url, name, 'scrollbars=yes,menubar=no,height=515,width=700,resizable=no,toolbar=no,status=no'); 就是它的作用
    • 啊,所以您可以通过以下方式获得访问权限:$ie.openwindow.winrefs["name"],其中“名称”应替换为最初为 name 变量传递给该代码的任何内容。
    猜你喜欢
    • 2010-10-12
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    相关资源
    最近更新 更多