locked
Login to site with Powershell and IE: Checking a form generated with javascript RRS feed

  • Question

  • <form name="frmLogin" action="/en/login.shtml" method="post"> <input type="hidden" name="login_attempt" value="yes"> <input type="hidden" name="redirect" value="/en/index.shtml"> <input type="text" name="login_username" class="txtBox1" title="Enter your user name" id="username" value="User name" onfocus="clickLoginField(this);" onkeypress="javascript:if ((event.which &amp;&amp; event.which == 13)||(event.keyCode &amp;&amp; event.keyCode == 13)) {document.frmLogin.login_password.focus(); return false;}"> <input type="password" name="login_password" class="txtBox2" title="Enter your password" id="pass" value="" onkeypress="javascript:if ((event.which &amp;&amp; event.which == 13)||(event.keyCode &amp;&amp; event.keyCode == 13)) { document.frmLogin.submit(); return false;}"> <input type="text" id="login_password_" name="login_password_" class="loginattemptstyle" value=""> <a class="login" href="javascript:void(0);" onclick="document.frmLogin.submit();">Login</a><noscript>&lt;input type="submit" value="Login"/&gt;</noscript> <div class="clear"></div> <table width="100%" cellpadding="5"> <tbody><tr> <td><a href="/en/forgottenpassword.shtml" title="Have you forgotten your password?">Forgotten password?</a></td> <td><table width="100%"> <tbody><tr> <td><input type="checkbox" name="login_remember" style="margin-left: 0; margin-right: 5px;"></td> <td>Stay signed in</td> </tr> </tbody></table></td> </tr> </tbody></table> </form>

    How can I login to a website via PowerShell. 

    About is the html code for website login form.

    Thanks

    Tuesday, June 11, 2013 2:51 PM

Answers

  • $username = "username"
    $password = "password"

    $ie = New-Object -com InternetExplorer.Application

    $ie.visible=$false

    $ie.navigate("Url")

    while($ie.ReadyState -ne 4) {start-sleep -m 100}

    $ie.document.getElementById("username").value= "$username"

    $ie.document.getElementById("pass").value = "$password"

    $ie.document.getElementById("loginform").submit()

    start-sleep 20

    $ie.Document.body | Out-File -FilePath c:\web.txt

    -----------

    Try the above. using the getelementbyid

    Elementid in your form for username is  username

    and for password is "pass"

    Please change the "url"

    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.


    • Edited by mohdazam89 Tuesday, June 11, 2013 3:00 PM
    • Marked as answer by Zuberv Tuesday, June 11, 2013 3:32 PM
    Tuesday, June 11, 2013 2:56 PM
  • it works just the changed
    "loginform").submit() to "frmLogin").submit()

    • Marked as answer by Jeremy_Wu Wednesday, June 12, 2013 6:53 AM
    Tuesday, June 11, 2013 3:33 PM

All replies

  • $username = "username"
    $password = "password"

    $ie = New-Object -com InternetExplorer.Application

    $ie.visible=$false

    $ie.navigate("Url")

    while($ie.ReadyState -ne 4) {start-sleep -m 100}

    $ie.document.getElementById("username").value= "$username"

    $ie.document.getElementById("pass").value = "$password"

    $ie.document.getElementById("loginform").submit()

    start-sleep 20

    $ie.Document.body | Out-File -FilePath c:\web.txt

    -----------

    Try the above. using the getelementbyid

    Elementid in your form for username is  username

    and for password is "pass"

    Please change the "url"

    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.


    • Edited by mohdazam89 Tuesday, June 11, 2013 3:00 PM
    • Marked as answer by Zuberv Tuesday, June 11, 2013 3:32 PM
    Tuesday, June 11, 2013 2:56 PM
  • Hi thanks for reply.

    I have edited username,password and user I tried executing the script in PS.

    I get following error

    PS C:\Users\myname\Desktop> .\new.ps1
    You cannot call a method on a null-valued expression.
    At C:\Users\myname\Desktop\new.ps1:9 char:1
    + $ie.document.getElementById("loginform").submit()
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
    
    PS C:\Users\myname\Desktop> http://www.desistream.tv/en/index.shtml

    Can you suggest something thanks.

    Tuesday, June 11, 2013 3:10 PM
  • it works just the changed
    "loginform").submit() to "frmLogin").submit()

    • Marked as answer by Jeremy_Wu Wednesday, June 12, 2013 6:53 AM
    Tuesday, June 11, 2013 3:33 PM