Eric Berlin
Eric Berlin

Reputation: 31

Simulating a Mouse Click in Powershell

I already know Powershell is not really meant for GUI automation, but just the same I have a pretty large Powershell script that needs to perform a single GUI operation, that being a left mouse click. I have searched for hours and have not found a satisfactory solution. The closest I have found is an add on called Wasp, but it was written in 2009 and appears to be incomplete anyway.

Is there another add in available to solve this problem?

Upvotes: 2

Views: 5313

Answers (1)

nkasco
nkasco

Reputation: 335

I cannot condone using this as a first resort, but it should work. Generally when dealing with web pages you want to use URL parameters as much as possible since webpage elements are volatile.

With that said you should be able to create an IE comobject and then select the element that way. For example:

First create an IE object:

$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.navigate("YourPageURLHere.com")
$ie.visible = $true

You can manipulate certain elements by ID (ex: name="username") or even type. These can be easily found be viewing the source code of a webpage. Here are a few examples:

$Username = $ie.document.getElementByID("Username")
$Username.value = $UsernameValue

$SearchButton = $ie.Document.getElementsByTagName("button")
$SearchButton.click()

Even if the webpage changes, since we have it all attached to the IE object you can then grab the output elements that generate after the submit button is clicked and feed them back into your script.

Upvotes: 1

Related Questions