Powershell网站自动化:Javascript弹出框冻结 [英] Powershell Website Automation: Javascript Popup Box Freeze

查看:110
本文介绍了Powershell网站自动化:Javascript弹出框冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动化用户从网站下载文件摘录的过程。我的问题是,一旦单击导出按钮,javascript弹出窗口会出现提取的各种日期参数以及需要单击的最终DOWNLOAD按钮。在这个javascript弹出窗口期间出现问题。单击导出按钮并打开弹出窗口后,PowerShell似乎在脚本期间冻结,并且不允许执行任何其他命令。

I am trying to automate a process in which the user downloads a file extract from a website. My issue is that once the 'export' button in clicked, a javascript popup window comes up with various date parameters for the extract along with the final 'DOWNLOAD' button that needs to be clicked. The problem arises during this javascript popup window. Once the export button is clicked and the popup window opens, powershell seems to freeze up during the script and will not allow any further commands to be executed.

我尝试使用WASP模块和SELECT-WINDOW来获取正确的IE进程窗口,但是'Select-Window -Title'名称是POPUP WINDOW - Windows Internet Explorer|选择-First 1 | Set-WindowActive'命令将不会执行,因为Powershell在单击初始导出按钮后立即卡住运行脚本(当弹出窗口打开时)。有没有办法可以在打开弹出框之后'刷新'Powershell以执行更多命令,或者有什么方法可以突破我在这个Javascrip弹出窗口中遇到的连续运行脚本循环窗口?

I have tried using the WASP module along with SELECT-WINDOW in order to grab the correct IE process window, however the 'Select-Window -Title "NAME OF POPUP WINDOW - Windows Internet Explorer" | Select -First 1 | Set-WindowActive' command will not execute as Powershell gets stuck 'Running script' immediately after the initial 'export' button is clicked (right when the popup window opens). Is there a way I can either 'refresh' Powershell after the popup box is opened in order to execute further commands, or any ideas on a way to break out of the continuous 'Running script' loop that I get stuck in with this Javascrip popup window?

# 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" }


推荐答案

自动化GUI浏览器时出现了很多问题 - 像你所描述的JS弹出窗口这样的模态对话框会阻止你的脚本完成,直到有人解散它为止。这是因为您在浏览器中进行的调用(导致模式对话框出现)也会阻塞,等待来自用户的交互。

This problem comes up a lot when automating GUI browsers - modal dialogs like the JS popup you describe will block your script from finishing until someone dismisses it. This is because the call you made into browser itself (which caused the modal dialog to appear) is also blocking, waiting for interaction from the user.

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

In my experience the easiest way to deal with this scenario is to launch a separate process just to handle the modal dialog. It takes two steps:


  1. 编写一个小型独立脚本/程序,等待模态对话框出现,然后将其关闭。通过手动启动弹出处理脚本,然后手动执行使对话框显示的步骤,可以很容易地进行测试。

  1. Write a small standalone script/program that will wait for the modal dialog to appear, then dismiss it. This is easy to test by launching the popup-handling script manually, then manually performing the steps that make the dialog appear.

一旦有了这个工作,就可以了自动化浏览器的主程序,在执行创建模态对话框的代码行之前启动单独的对话框处理脚本。

Once you have that working, in your main program that automates the browser, launch your separate dialog-handling script just before you execute the line of code that creates the modal dialog.

就是这样。它感觉很笨,但它几乎可以在我见过的任何情况下工作(除非模态对话需要提升的安全权限,这会带来其他问题)。

And that's it. It feels clunky but it will work in pretty much any case I've seen (unless the modal dialog requires elevated security privileges, which brings up other issues).

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

Also, if you're using a programming language that has native thread support, you can launch a separate thread (instead of a process) to handle modal dialogs.

一些测试自动机感觉如果可能的话,它实际上更便宜(在脚本开发和维护时间方面)绕过像这样的模态对话框,假设您可以绕过它并仍然对测试有信心。有时这可能需要修改应用程序,以便为测试代码提供一个特殊的系统接口。

Some test automators feel it's actually cheaper (in terms of script development and maintenance time) to simply bypass modal dialogs like this if possible, assuming you can get around it and still feel confident in your testing. Sometimes this might require modifying the application to give your test code a special interface into the system.

这篇关于Powershell网站自动化:Javascript弹出框冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆