title: How to acess javascript-based dialog window in web page with Windows Powershell?
body: <p>I'm accessing an old web page, poorly scripted, with poorly identified elements. Until now, I could access all objects it gave me. But now, I'm stucked. </p>
  
  <p>When I click this button, it opens a JavaScript dialog, in which the user must insert some values. And I just click at it. But when the window opens, I just can't focus at it. </p>
  
  <p>This is the code that creates it:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>&lt;INPUT onclick="javascript: openCosts(document.forms[0], 'COMPANY');" 
  title="Include values" 
  style="BORDER-LEFT-WIDTH: 1px; CURSOR: hand; FONT-SIZE: 11px; FONT-FAMILY: Tahoma; BORDER-RIGHT-WIDTH: 1px; 
  BORDER-TOP-COLOR: #007041; BORDER-BOTTOM-WIDTH: 1px; BORDER-LEFT-COLOR: #007041; FONT-WEIGHT: bold; 
  COLOR: #fff; PADDING-BOTTOM: 1px; BORDER-BOTTOM-COLOR: #007041; PADDING-TOP: 1px; PADDING-LEFT: 3px; 
  BORDER-RIGHT-COLOR: #007041; PADDING-RIGHT: 3px; BORDER-TOP-WIDTH: 1px; BACKGROUND-COLOR: #0080ff" 
  type=button value="Costs - SAP" name=btn_show_costs&gt;&lt;/INPUT&gt;
  </code></pre>
  
  <p>I tried to send TABS and ENTERS; tried to get all open IExplorer open windows -- $shell = New-Object -ComObject Shell.Application and Get-Process | Where-Object ... doesn't work. </p>
  
  <p>Can somebody help me??</p>
  
  <p>That's the resumed code, until now...</p>
  
  <pre><code>$ie= new-object -ComObject "InternetExplorer.Application"
  $ie.visible=$true
  $ie.Navigate("an_local_asp_web_page.asp")
  
  while($ie.ReadyState -ne 4) {start-sleep -m 100}
  
  $ie.Document.getElementById("frame_principal").contentDocument.getElementById("cbb_profile")[2].selected = $true
  $ie.Document.getElementById("frame_principal").contentDocument.getElementsByTagName("IMG")[1].click()
  
  start-sleep -m 2000
  
  $image = $ie.Document.getElementById("frame_principal").contentDocument.getElementsByTagName("IMG") | Where-Object { $_.href -like "*img_menu.gif*" }
  $image.click() 
  
  start-sleep -m 2000
  
  $ie.Document.getElementById("frame_principal").contentDocument.getElementById("menu_vehicle").contentDocument.getElementsByTagName("IMG")[0].click()
  
  start-sleep -m 1000
  
  $ie.Document.getElementById("frame_principal").contentDocument.getElementById("frame_body").contentDocument.getElementsByName(“txt_user”)[0].value = "NICHOLAS"
  $ie.Document.getElementById("frame_principal").contentDocument.getElementById("frame_body").contentDocument.getElementsByName(“rd_driver”)[0].Click()
  $ie.Document.getElementById("frame_principal").contentDocument.getElementById("frame_body").contentDocument.getElementsByName(“btn_show_costs”)[0].Click() 
  
  start-sleep -m 1000
  
  # Now, that next part should work... But it doen't. It says 
  # 'You cannot call a method on a null-valued expression.'
  # I only see the IE Window that generates the Dialog Window... 
  $app = New-Object -ComObject Shell.Application
  $app.Windows()
  $window = $app.Windows() | where {$_.LocationName -like "*show costs*"} 
  $image = $window.Document.getElementsByTagName("IMG") | Where-Object { $_.href -like "*bt_add_cost.gif*" }
  $image.click()
  
  start-sleep -m 1000
  
  $window.Document.getElementById("rate_cd_cbi").Value = "COSTCENTER"
  $window.Document.getElementById("rate").Value = "100"
  $window.Document.getElementById("btn_include").Click()
  </code></pre>
  
  <p>Thank you in advance!</p>