webpage button.click() not working

Welcome Forums General PowerShell Q&A webpage button.click() not working

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
1 year, 2 months ago.

  • Author
    Posts
  • #80894

    Participant
    Points: 64
    Rank: Member

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

    $cam1 = Invoke-WebRequest -Uri "http://10.2.1.221/doc/page/login.asp?_1506456198818"
    $userName = $cam1.ParsedHtml.getElementById('username').value = "cam1"
    $password = $cam1.ParsedHtml.getElementById('password').value = "P@4$w0rd*"
    $loginButton1 = $cam1.ParsedHtml.body.getElementsByClassName("login-item anonymous")
    $loginButton1.click()

    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 
    'click'.
    + $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

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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.

The topic ‘webpage button.click() not working’ is closed to new replies.