Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell UI Automation within a webbrowser control

Ok so I recently learned how to execute UI automation using the $ie = new-object -com "internetexplorer.application" command. This works pretty good for a task I am attempting to automate on an ASPX web app...until I ran into a modal popup which freezes my script. From what I have read online, I may want to bypass this by running this same process on a form, using a forms.webbrowser control.Sample Follows

    [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
    [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    $form = New-Object Windows.Forms.Form
    $form.text = "My Form"
    $form.size = New-Object Drawing.size @(700,600)
    $web = New-object System.Windows.Forms.webbrowser
    $web.location = New-object System.Drawing.Point(3,3)
    $web.minimumsize = new-object System.Drawing.Size(20,20)
    $web.size = New-object System.Drawing.size(780,530)
    $web.navigate("http://myASPXsite.com")
    $form.Controls.Add($web)
    $form.showdialog()
    $doc = $web.document
    $doc.getElementbyID('txtUserName').value = "myUser"

The point is that the ability to reference and invoke html elements doesn't work through this method. I can navigate to the webpage but nothing more. Could it be that the webbrowser control doesn't have the same functionality as the internetexplorer object. This is the code I would use with the other method.

   $ie = new-object -com "internetexplorer.application"
   $ie.navigate("http://www.MyASPXsite.com")
   While ($ie.readystate -ne 4) {start-sleep -m 100}
   $doc = $ie.document
   $doc.getelementbyid('txtUserName').value = "myUser"

Any ideas on my situation? Am I even on the right track to handle the modal dialogs?

like image 299
Scriptd Avatar asked Mar 02 '26 19:03

Scriptd


1 Answers

Your problem is $form.showdialog(). The script will hold up in this point until you close the form. You have to add an event handler to the form's Shown event something like this:

$form.Add_Shown({
  $doc = $web.document;
  $doc.getElementbyID('txtUserName').value = "myUser";
});

Then it should work

like image 101
Adam Avatar answered Mar 04 '26 17:03

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!