Login to URL page use powershell script

Alex Corovin 46 Reputation points
2021-01-14T16:30:38.637+00:00

Hi Everyone,

Could you please help me to login to the page I use this script below, the script only works before opening the site and added credentials but can not click see below error

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible

$username="username"

$password="password"

$ie.Navigate("https://domain.com/login")

While ($ie.Busy -eq $true) {Start-Sleep -Seconds 10;}

$usernamefield = $ie.document.getElementByID('lemail')
$usernamefield.value = "$username"

$passwordfield = $ie.document.getElementByID('password')
$passwordfield.value = "$password"

$Link = $ie.document.getElementByID('login')
$Link=$ie.Document.getElementsByTagName("button") | where-object {$_.type -eq "Log In"}
$Link.click()
error

You cannot call a method on a null-valued expression.
At line:20 char:1

  • $Link.click()
  • ~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull

see below Inspect Button Log in

56704-2021-01-14-11-24-59-login.png

thank you very much for your help,

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} vote

3 answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-01-15T07:38:12.473+00:00

    Hi,

    In your source file, "Log In" is not the type of the button but the text of the span. See if this works for you.

    $link = $ie.document.documentElement.getElementsByTagName('span') | where-Object {$_.textContent -eq 'Log In' }  
    $link.click()  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-01-16T16:47:07.223+00:00

    Hi,

    Try simulating keyboard input SendKeys

    do {  
        Start-Sleep -Seconds 1  
    }  
    while ($ie.busy)  
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")  
    [Microsoft.VisualBasic.Interaction]::AppActivate("Internet Explorer")  
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")  
    [System.Windows.Forms.SendKeys]::Sendwait("{RIGHT}{RIGHT}{ENTER}")  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  3. Alex Corovin 46 Reputation points
    2021-01-15T20:55:19.687+00:00

    thank you for you help, could you help me please how i can click save by powershell script in IE

    57098-2021-01-15-15-53-57-login-to-url.png

    thank you

    0 comments No comments