htmlpowershell

Button click is not working using powershell


i am adding user id and password to website and trying to login to site. in below code i am not able to click on the Login button i tried click submit but not able to click. can any one please help me

$username = "abcd" 
$password = "abc" 
$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$true
$ie.navigate("https://www.google.com/") 
while($ie.ReadyState -ne 4) {start-sleep -m 100} 
$ie.document.getElementById("loginId").value= "$username" 
$ie.document.getElementById("password").value = "$password" 
$fs = $ie.document.getElementById("loginButton")
$fs.click()

Solution

  • Instead of native getElementById method, use IHTMLDocument3_getElementById:

    $username="myname"
    $password="mypass"
    $url = "https://www.google.com/"
    $ie = New-Object -com InternetExplorer.Application
    $ie.visible = $true
    $ie.navigate($url)
    while($ie.Busy) { Start-Sleep -Milliseconds 100 }
    $ie.document.IHTMLDocument3_getElementById("loginId").value = $username
    $ie.document.IHTMLDocument3_getElementById("password").value = $password
    $ie.document.IHTMLDocument3_getElementById("loginButton").click()
    

    It will show an alert stating "Login Name and Password combination is incorrect.".

    It's working on my machine having IE11 and WIN10. There are 3 important changes in above code: