1

I'm trying to automate a simple web form login and capture the output and values from elements on a page using PowerShell (using getElementById/TagName).

I know there are examples of people using the InternetExplorer.Application COM object, but I know .net comes with the System.Windows.Forms.WebBrowser object.

My questions are - is it possible to do this UI Automation with just .net objects by loading WebBrowser object in memory and have it navigate to my URL then run my logic after navigation is complete? Does this object need to be added to a Windows form before it begins to do any processing?

Here's what I'm somewhat trying to do:

[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$browser = New-Object System.Windows.Forms.WebBrowser
$browser.Navigate("http://somesite.net/loginPage")
do {
    Start-Sleep -Seconds 1
}until($browser.ReadyState -eq 'complete')
$txtUsername = $browser.Document.getElementsByTagName('txtuser')
$txtPassword = $browser.Document.getElementsByTagName('txtpass')
# Perform more stuff here
Matthieu
  • 2,736
  • 4
  • 57
  • 87
Jeff Coe
  • 482
  • 3
  • 18
  • I don't know the answer to your question, but I wonder if you've looked at [`Invoke-WebRequest`](https://technet.microsoft.com/en-us/library/hh849901.aspx?f=255&MSPPError=-2147217396)? – briantist Feb 25 '15 at 18:51
  • Yeah, I definitely have... For many reasons, it's easier to use the COM objects and parse the document and automate it that way. I could use the COM object, but I was curious if I could do the same thing with the .net objects. – Jeff Coe Feb 25 '15 at 18:55
  • It's possible, but you need to organize a message loop for `WebBrowser`. E.g, (in c#): http://stackoverflow.com/a/22262976/1768303 – noseratio Feb 25 '15 at 22:25

0 Answers0