PowerShell的网站自动化:JavaScript弹出框冻结

问题描述 投票:1回答:3

我想自动在用户下载某网站的文件提取物的方法。我的问题是,一旦在单击“导出”按钮,一个javascript弹出窗口出现了各种日期参数与最终的“下载”按钮需要点击沿提取物。此javascript弹出窗口时,就会出现问题。一旦导出按钮被点击并在弹出的窗口中打开,PowerShell中似乎脚本期间冻结,不会允许执行的任何进一步的命令。

我一直在使用,以便抓住正确的IE进程窗口中选择-WINDOW沿着WASP模块试过,但“选择-窗口-Title‘弹出窗口的名称 - 的Windows Internet Explorer’|选择 - 第一1 |设置WindowActive”命令将不执行的PowerShell的卡住‘行书出口‘按钮被点击’初始后立即’(弹出窗口打开时,右边)。有没有一种方法,我既可以弹出框,以便在方式打破了,我得到这个的javascrip弹出停留在连续“行书”循环的执行进一步的命令,或任何想法打开“刷新” Powershell的后窗口?

# Set access URL and Credentials
$WebU='username'
$WebP='password'
$URLlogin='http://websiteurl.com'
$IEwait="1000"
# Execute IE and navigate to URL
$ie = New-Object -com "InternetExplorer.application";
$ie.visible = $True;
$ie.navigate($URLlogin);

Try{
# Login steps
while ($ie.Busy ) { Start-Sleep -Milliseconds $IEwait; }
if ($ie.Document.getElementByID("txtLogin"))
    { $ie.Document.getElementByID("txtLogin").value = $WebU }
while ($ie.Busy ) { Start-Sleep -Milliseconds $IEwait; }
if ($ie.Document.getElementByID("txtPassword"))
    { $ie.Document.getElementByID("txtPassword").value = $WebP }
while ($ie.Busy ) { Start-Sleep -Milliseconds $IEwait; }
if ($ie.Document.getElementByID("btnLogin"))
    { $ie.Document.getElementByID("btnLogin").Click() }
# Navigate through website to find the 'Export' button
while ($ie.Busy ) { Start-Sleep -Milliseconds $IEwait; }
if ($ie.Document.getElementByID("managementMenu_btnLearningResults"))
    { $ie.Document.getElementByID("managementMenu_btnLearningResults").Click() } 
while ($ie.Busy ) { Start-Sleep -Milliseconds $IEwait; }
# This is the part that freezes the script. Once the 'btnExport' button is clicked, a JS popup keeps Powershell from executing any additional commands
if ($ie.Document.getElementByID("btnExport"))
    { $ie.Document.getElementByID("btnExport").Click() } 
# Once the popoup box opens, sending the 'ENTER' key should automatically download the export file
Select-Window -Title "NAME OF POPUP WINDOW - Windows Internet Explorer" | Select -First 1 | Set-WindowActive
Select-Window -Title "NAME OF POPUP WINDOW - Windows Internet Explorer" | Select-childwindow | Send-Keys "{ENTER}"
}
# These won't execute because the JS popup window confuses Powershell and keeps it in a continuous 'Running Script' process
Catch {
    "Error" }
Finally {
    "Now able to execute further commands" }
javascript powershell automation
3个回答
1
投票

模态对话框像你描述会挡住你的脚本完成,直到有人将它关闭了JS弹出 - 自动化GUI浏览器时,这个问题出现了很多东西。这是因为叫你到浏览器本身(这导致出现模式对话框)也被阻塞,等待来自用户交互作出。

根据我的经验来处理这种情况的最简单方法是只需启动一个单独的进程来处理模态对话框。这需要两个步骤:

  1. 写一个独立的小脚本/程序,将等待模式对话框出现,然后关闭它。这是很容易通过手动手动启动弹出处理脚本,然后执行,使该对话框中出现的步骤进行测试。
  2. 一旦你的工作,你的主程序,它可以自动浏览器,启动你执行代码创建模式对话框之前的行的单独的对话框处理脚本。

仅此而已。感觉笨重,但它几乎我见过的(除非模式对话框需要提升的安全权限,这带来了其他问题)任何情况下工作英寸

另外,如果你使用的是具有本地线程支持的编程语言,可以启动一个单独的线程(而不是过程)来处理模式对话框。

有些测试自动化觉得它实际上更便宜(在脚本的开发和维护时间方面)简单地绕过模式对话框这样如果可能的话,假设你可以避开它,还是觉得在您的测试充满信心。有时候,这可能需要修改应用程序,让您的测试代码的专用界面到系统中。


0
投票

IE的对象模型实际上是有种诡谲。它经常说的东西准备好时,他们都没有,因为基础资源尚未加载。我恼火,每次我不得不与网页互动重写这段代码,所以我写了一个叫做自动浏览模块来对付它:

http://autobrowse.start-automating.com/

使用此相反,你会回避这个问题。

希望这可以帮助


0
投票
    While ( $ie.busy -eq $true){ 
        [System.Threading.Thread]::Sleep(2000); 
        $wshell = New-Object -ComObject wscript.shell;
        if($wshell.AppActivate('Message from webpage'))
        {
            [System.Threading.Thread]::Sleep(1000); 
            [System.Windows.Forms.SendKeys]::SendWait('{Enter}')
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.