user694485
user694485

Reputation: 121

Click on <BUTTON> in HTML page

<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>

Upvotes: 2

Views: 15735

Answers (2)

user694485
user694485

Reputation: 121

the closest I could get to this type of automation was by writing this:

$Click=$ie.Document.getElementsByTagName("button") | Where-Object {$_.type -eq 'submit'}
$Click.click();

this code clicked all the buttons on the page but did get me logged into the webpage.

on further reading(http://www.w3schools.com/jsref/met_namednodemap_item.asp) I have been tweaking the code using:

$Link = $ie.Document.getElementsByTagName("button")| where-object {$_.type -eq "submit"} $Link.item(2).click()

Upvotes: 0

Aman Sharma
Aman Sharma

Reputation: 1990

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();

Upvotes: 1

Related Questions