7

I am writing some Internet Explorer automation scripts using PowerShell. Here is how I start the IE com object:

$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate("about:blank")
$ie.visible = $true

$doc = $ie.Document

So, what I would like to do is to execute some javascript on the $doc object. For example, I have an item on the page that has an onclick event which executes submitCommand('lookup'), so I'd like to run that directly on the $doc instead of having to find the object on the page and then calling the Click() method on it.

It would be easier as the object has no name nor id, making it very sensible to change as I can only rely on it's position on the page (eg: the 11th span item on the page).

Alternatively, how would you select elements based on their class? That would help a lot as the "button" has it's own class.

Thanks

2
  • Alternatively, how would you select elements based on their class? That would help a lot as the "button" has it's own class. Commented Sep 18, 2009 at 12:45
  • 2
    @Philippe: Instead of commenting your own question with more info and questions why not simply edit the existing question? Commented Sep 18, 2009 at 12:49

1 Answer 1

5

$spans=@($ie.document.getElementsByTagName("SPAN"))

Pipe to where-object to filter the one you need (based on its attributes) and then call the click method, for example:

$span11 = $spans | where {$_.innerText -eq 'something'}
$span11.click()
Sign up to request clarification or add additional context in comments.

3 Comments

Not exactly the solution I was hoping for, but definitely a cleaner solution to my issue. Thanks.
By the way, is there any way to store the result of the where in an array? In case there are more than one result to the filtering, only the first one is returned if you store it in a variable like this?
You can wrap it in '@()': $span11 = @($spans | where {$_.innerText -eq 'something'})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.