AVB
AVB

Reputation: 11

Powershell: How to press a button on a web page if you don't know the button ID

I'm trying to fill in my username and password on a certain web page and then the press the "sign-in" button, all automatically via Powershell.

The problem is that I cannot find the ID of the sign-in button in the html source code.

I have tried to use the command:

$link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -match 'sign in'}

but this didn't worked either.

Any thoughts which PS command to use if I want to press the 'sign in' button?

The HTML code is as follows:

<div class="login">
    <div class="content">
        <div class="subsection-1">Sign in or create a <a id="ctl00_HeaderUserControl_LoginUserControl_hypNewAccount" href="/authentication/registerprovider.aspx?">new account</a></div>
        <div class="subsection-2">
            <span class="navy bold">What is your email address?</span>
            <div class="indent">
                <span class="bold">Email:</span>
                <input id="loginEmail" class="text-box" type="text" maxlength="100" tabindex="1" />
                <img class="ajax-loader" src="/content/images/research/global/transparent.gif" alt="" />
            </div>
            <div class="invalid-email"></div>
        </div>
        <div class="subsection-3">
            <span class="navy bold">Do you know your password?</span>
            <div>
                <input id="passwordNotKnown" name="passwordSwitch" type="radio" checked="checked" />
                <label for="noPassword">No, I don't know my password or I'm new .</label>
            </div>
            <div>
                <input id="passwordKnown" name="passwordSwitch" type="radio" />
                <label for="noPassword">Yes, my password is:</label>
                <input id="loginPassword" class="text-box" type="password" maxlength="50" tabindex="2" />
            </div>
            <div class="invalid-password"></div>
            <div class="error"></div>
        </div>
        <div class="subsection-4">
            <button type="button" class="login-button greenButton" tabindex="4">Sign in</button>
            <a href="javascript:void(0);" class="cancel-link">Cancel</a>
            <input id="stayLoggedIn" type="checkbox" tabindex="3" />
            <label for="stayLoggedIn">Stay signed in</label>
        </div>
    </div>
    <div class="reset-password">
        <div class="subsection-1">Would you like to reset your password?</div>
        <div class="subsection-2">Choosing <span class="bold">yes</span> will reset your password and email a temporary password to: <span class="repeat-email"></span></div>
        <div class="subsection-3">
            <div class="center">
                <button class="reset-password-button" type="button">Yes</button>
                <button class="do-not-reset-password-button" type="button">No</button>
            </div>
        </div>
    </div>
</div>

Upvotes: 0

Views: 12066

Answers (2)

user3746491
user3746491

Reputation: 31

I came here looking for a similar answer and figured out how to get this done. Posting here for any future searchers. As noted by Szymon, getElementsByClassName returns an array, and you will need to select the item you need before the click. In my case the classname was button and I was able to click it using the following:

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

This works for me because there is only the one item with the classname button, so your results my vary if you have more than one with the same name. Just change what you grab using select-object if need be.

Upvotes: 3

Szymon Kuzniak
Szymon Kuzniak

Reputation: 858

As @Jamie-Taylor mentioned the login button is in fact a button.

You can access it not only by the id but also by the class name using document.documentElement.getElementsByClassName. Please notice this function will return list of elements as more than one element on page can have the same class.

EDIT I was wrong: in order to have getElementsByClassName you have to call it on document.documentElement instead of document

Upvotes: 2

Related Questions