Farhan
Farhan

Reputation: 101

button click on website 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://wss.mahadiscom.in/wss/wss?uiActionName=getCustAccountLogin") 
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()

Upvotes: 1

Views: 5483

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

Instead of native getElementById method, use IHTMLDocument3_getElementById:

$username="myname"
$password="mypass"
$url = "http://wss.mahadiscom.in/wss/wss?uiActionName=getCustAccountLogin"
$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:

  • Using http rather than https because of IE problem with certificate of that site at the time which I tried the site.
  • Checking Busy property of IE to check if IE is busy, then wait for it.
  • Using IHTMLDocument3_getElementById instead of getElementById.

Upvotes: 1

Related Questions