webpage button.click() not working

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 2 weeks, 6 days ago.

  • Author
  • #80894

    Tony Antony

    I have this code to login to a ip camera with the username and password.

    $cam1 = Invoke-WebRequest -Uri ""
    $userName = $cam1.ParsedHtml.getElementById('username').value = "cam1"
    $password = $cam1.ParsedHtml.getElementById('password').value = "P@4$w0rd*"
    $loginButton1 = $cam1.ParsedHtml.body.getElementsByClassName("login-item anonymous")

    But, when I call $loginButton1.click(), it gives the message below. According the the Get-Member, $loginButton1 have a method called click.

    Method invocation failed because [System.__ComObject] does not contain a method named 
    + $loginButton1.click()
    + ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (click:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
    PS C:\WINDOWS\system32> $loginButton1 | Get-Member
       TypeName: System.__ComObject#{3050f50c-98b5-11cf-bb82-00aa00bdce0b}
    Name                         MemberType Definition                                          
    ----                         ---------- ----------                                          
    addBehavior                  Method     int addBehavior (string, Variant)                   
    addFilter                    Method     void addFilter (IUnknown)                           
    appendChild                  Method     IHTMLDOMNode appendChild (IHTMLDOMNode)             
    applyElement                 Method     IHTMLElement applyElement (IHTMLElement, string)    
    attachEvent                  Method     bool attachEvent (string, IDispatch)                
    blur                         Method     void blur ()                                        
    clearAttributes              Method     void clearAttributes ()                             
    click                        Method     void click ()   
  • #81146

    Don Jones

    Yeah, I've run into that sometimes. The problem is you're using an ancient COM interface which is flowing through several layers of COM interop to get into .NET, and then more layers to get into PowerShell. It sometimes just doesn't work. The COM object itself is deprecated and hasn't had any changes in almost a decade at this point.

    I run into this the most when getting elements by class name – sometimes you'll get more than one element, which means you technically have a collection, which doesn't have a click() method. Get-Member unwinds collections, so it's kind of lying to you by showing you the contents rather than the actual kind of object $loginButton1 is. You might try getting it by ID, or try $loginbutton1[0] to refer to a specific element.

You must be logged in to reply to this topic.