Author Posts

December 8, 2016 at 10:00 pm

Hi,
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
$ie.Navigate($url)

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")
$submitButton.click();

Start-Sleep 2

$Link1= $doc.getElementByID("Link1")
$Link1.click();

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

Start-Sleep 10
$Link2= $doc.getElementByID("Link2")
$Link2.click();

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

Start-Sleep 5
$Link3= $doc.getElementByID("Link3")
$Link3.click();

Please let me know where did i make a mistake.

Thanks in Advance

Nagaraju

December 8, 2016 at 10:06 pm

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.

December 8, 2016 at 10:15 pm

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.

Thanks

December 8, 2016 at 11:27 pm

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

December 9, 2016 at 2:59 am

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.