Click on <BUTTON> in HTML page

13,855

In the code you shared the $doc variable is not getting initialized. Please initialize it first before using it in the below line:

$doc.documentElement.getElementsByClassName('button')

The error on this line, i.e. null-valued expression error, is due to the fact that $doc is null at this point.

Alternate Solution: Instead try the below code to get the submit button in the form on the page. You can tweak the code as per your requirement and the page you are targeting.

$submitButton=$ie.Document.getElementsByTagName("button") | Where-Object {$_.innerhtml -eq 'Login'}
$submitButton.click();
Share:
13,855

Related videos on Youtube

user694485
Author by

user694485

Updated on September 16, 2022

Comments

  • user694485
    user694485 over 1 year
    <button type="submit">Login</button>
    

    I am trying to click/submit the above button using Windows Powershell. I have tried the following code:

    $submitButton = $doc.documentElement.getElementsByClassName('button') |
                    Select-Object -First 1
    $submitButton.click()
    

    which brings back this error:

    You cannot call a method on a null-valued expression.
    ps1:15 char:1
    +     $submitButton = $doc.documentElement.getElementsByClassName('button') | ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
    
    You cannot call a method on a null-valued expression.
    ps1:16 char:1
    + $submitButton.click()
    + ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    I also tried this code:

    $loginBtn = $ie.Document.getElementsById('input') |
                Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'LoginButton'}
    $loginBtn.click()
    

    but this also brings back the same error as before.

    my powershell code in full:

    $username='USERNAME' 
    $password='PASSWORD'
    
    $ie = New-Object -ComObject 'internetExplorer.Application'
    $ie.Visible= $true
    $ie.Navigate("URL EXAMPLE")
    
    while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}   
    
    $Open = $ie.Document.getElementByID('has-account')
    $Open.click()
    
    $usernamefield = $ie.Document.getElementByID('login-usr').value =          $username
    $passwordfield = $ie.Document.getElementByID('login-pass').value =     $password
    $submitButton = $doc.documentElement.getElementsByClassName('button') |     Select-Object -First 2
    $submitButton.click() 
    

    EDIT

    Here is the output of powershell getelementbytagname. Notice there is no classname or ID, how does it get clicked?

    className                    : 
    id                           : 
    tagName                      : BUTTON
    parentElement                : System.__ComObject
    style                        : System.__ComObject
    document                     : mshtml.HTMLDocumentClass
    sourceIndex                  : 60
    offsetLeft                   : 61
    offsetTop                    : 220
    offsetWidth                  : 320
    offsetHeight                 : 41
    offsetParent                 : System.__ComObject
    innerHTML                    : Login
    innerText                    : Login
    outerHTML                    : <button type="submit">Login</button>
    outerText                    : Login
    parentTextEdit               : System.__ComObject
    

    EDIT

    <form id="sp-login-form" action="#" method="post">
            <button type="button" id="fb-login-btn" class="button fb">Log in with Facebook<span style="position: absolute;"></span></button>
            <em>or</em>
            <div>
              <label for="login-usr">Username</label>
              <input type="text" name="username" id="login-usr" placeholder="Spotify username">
              <label for="login-pass" class="pass">Password</label>
              <input type="password" name="password" id="login-pass" class="pass" placeholder="Password">
              <button type="submit">Login</button>
            </div>
          </form>
    
  • user694485
    user694485 almost 8 years
    ok, thank you. what could I use in its place to click this submit button?
  • Aman Sharma
    Aman Sharma almost 8 years
    I would recommend to add an ID to the submit button if you are the developer for that page and then use $submitButton = $ie.Document.getElementsById() method using that ID.
  • Aman Sharma
    Aman Sharma almost 8 years
    @user694485 - I have updated my answer with a generic solution. If it helps please accept by clicking on the tick mark below the arrows beside the answer.
  • Aman Sharma
    Aman Sharma almost 8 years
    @user694485 I see that you have modified the code. Can you also provide the Html for your button on the web page? And also please don't forget to comment once you have modified. We are not notified of modifications.
  • user694485
    user694485 almost 8 years
    added html to question
  • Aman Sharma
    Aman Sharma almost 8 years
    I have updated the solution. Try it, it works. Please accept as answer and upvote by clicking on the tick mark and the up arrow beside my answer if this resolved your issue.
  • Aman Sharma
    Aman Sharma almost 8 years
    @user694485 Please check my updated answer. This works.