Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method invocation failed because [System.Object[]] doesn't contain a method named 'Click'

I'm running the below code with PowerShell and it runs successfully on one server but not the other. Both servers are running Windows Server R2 Standard with IE 8. The script runs in Admin mode, also in IE, "Enable Protected Mode" is turned off for Local Intranet.

$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true 
$ie.Navigate("http://localhost/testwebsite")
While ($ie.Busy) {Sleep 3}
$doc = $ie.Document
$btn = $doc.getElementsByTagName("input")
$Button = $btn | ? {$_.Name -eq "refreshBtn"}
$Button.Click()
$ie.Quit()

And here is the error I get on one of the boxes (note: don't receive this error on the other machine):

Method invocation failed because [System.Object[]] doesn't contain a method named 'Click'.

Is there a security setting I need to change on the server? Do I need to adjust my script? Anything else?

BTW: I've checked various posts on StackOverflow regarding issues related to this and so far I haven't found anything that's helped.

Thanks in advance!

like image 885
Keith Avatar asked Dec 03 '25 05:12

Keith


1 Answers

Not sure why its different, but it looks like your pipeline

$Button = $btn | ? {$_.Name -eq "refreshBtn"}

is returning multiple buttons, so $Button is actually an array. PowerShell 3 handles this better: it will actually call Click() on every element in the array. Upgrading to PowerShell 3 is probably not an option.

You can workaround this a couple ways. First, add the Click() method call to the pipeline which finds the button:

$doc.getElementsByTagName("input") | 
    Where-Object { $_.Name -eq "refreshBtn" } | 
    ForEach-Object { $_.Click() }

Of course, you may consider it an error if you're getting back multiple buttons. In that case, you'll want to handle it:

$Button = $doc.getElementsByTagName("input") | 
              Where-Object {$_.Name -eq "refreshBtn"}
if( $Button -is 'Object[]' )
{
    Write-Error ('Found multiple <refreshBtn> buttons.')
}
like image 182
Aaron Jensen Avatar answered Dec 06 '25 06:12

Aaron Jensen



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!