How do I get PowerShell to click the login button of a website?

This topic contains 1 reply, has 2 voices, and was last updated by  postanote 4 months ago.

  • Author
    Posts
  • #91469

    Kevin
    Participant

    I want to have a script log into Jenkins automatically. Using I.E. I have inspected some elements to create a PowerShell script to do almost everything that I want it to do. My script will launch I.E. and fill out the username and password correctly. How do I get the "Login" button to be clicked? I would rather not use SENDKEYS.

    Here is my script:

    $username = "jdoe";
    $password = "strongpassword";
    $loginUrl = "http://jenkinsserver:8080/"

    $ie = New-Object -com internetexplorer.application;
    $ie.visible = $true;
    $ie.navigate($loginUrl);
    while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; } #wait for browser idle

    ($ie.document.getElementsByName("j_username") | select -first 1).value = $username;
    ($ie.document.getElementsByName("j_password") | select -first 1).value = $password;
    ($ie.document.getElementsByName("login") | select -first 1).click();

  • #91475

    postanote
    Participant

    You do not say what is not working or post the error you are getting.

    I do this sort of thing for automate website testing as well as using the PoSH web cmdlets. Yet, the code you show here should work as defined. If you designed the login page, then you should use the *ByName or *ById from your page code. You need to call the right element of course. So, re-inspecting the page objects is important to make sure you are calling the right thing.

    For, example, on a default ADFS IdP page, each of the button are specified differently. Using this code below for a default ADFS idP login, works without issue.

        $ie = New-Object -com internetexplorer.application
        $ie.visible = $true
        $ie.navigate($loginUrl)
        while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1 }
    
        ($ie.document.getElementById('idp_GoButton') | select -first 1).click()
        Start-Sleep -Seconds 1 
    
        ($ie.document.getElementById('userNameInput') | select -first 1).value = $username
        Start-Sleep -Seconds 1 
    
        ($ie.document.getElementById('passwordInput') | select -first 1).value = $password
        Start-Sleep -Seconds 1 
    
        ($ie.document.getElementById('submitButton') | select -first 1).click()
        Start-Sleep -Seconds 1 
    
        ($ie.document.getElementById("idp_SignOutButton") | select -first 1).click()
        Start-Sleep -Seconds 1 
    

    Notice I am not using any of the additional semi-colons as you are, as they are only needed in certain cases. Generally I've seen folks do this out of habit from other languages or that is the way they7 were taught PoSH in a class/training session.

You must be logged in to reply to this topic.