title: Powershell Website Automation: Javascript Popup Box Freeze
body: <p>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. </p>
  
  <p>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? </p>
  
  <pre><code># 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" }
  </code></pre>