Powershell anchor tag not found after first click in the same page

I am trying to automate the website login and clicking few links after that. Everything looks good until login and first click, but after that i could not find the link control and getting null-valued exception.

In the Home page, i have 3 links. It works for the first link then after that it can’t find the 2nd link and so on.

Sample Code

$username = “xyz”
$password = “pwd”
$url = “http://abc.com/

Create an Internet Explorer object

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

Open the website


do {sleep 1} until (-not ($ie.Busy))

Assign the DOM to the $doc variable

$doc = $ie.document

Assign Username and Pwd

$usernameField = $doc.getElementById(‘username’)
$usernamefield.value = ‘username’

Start-Sleep 1

$passwordfield = $doc.getElementByID(‘Password’)
$passwordfield.value = ‘password’

Wait for 2 seconds before click on Login button

Start-Sleep 2

Click Login Button

$submitButton = $doc.getElementByID(“Login”)

Start-Sleep 2

$Link1= $doc.getElementByID(“Link1”)

while ($ie.Busy -eq $true){Start-Sleep -seconds 4;}

Start-Sleep 10
$Link2= $doc.getElementByID(“Link2”)

while ($ie.Busy -eq $true){Start-Sleep -seconds 4;}

Start-Sleep 5
$Link3= $doc.getElementByID(“Link3”)

Please let me know where did i make a mistake.

Thanks in Advance


Powershell is not the best choice to automate keyboard and mouse actions. There are easier tools to do something like this. What do you want to do on the website? Powershell is able to interact with websites/webservers.

We would like to display the website in the office break room tv which would display live data automatically. Please let me know if you know any tools to do the same.


Hmmm … browsers are able to display websites. :wink: Why do you need a script for that?

Not 100% sure but I think you may need to set the $doc variable again before clicking link 2 since after clicking link 1 the DOM would reload.

Try adding just another $doc=$ie.document right before your assignment of $link2 and the same for $link3.