1

Powershell script to open webpage and click a specific link

I am trying to write a script that opens up a specific webpage and clicks a specific link. I cannot edit the HTML as it is embedded on a device (Digital Projector). I have seen this question asked and answered before but I am unable to get it to work. I have even tried copying code from other serverfault answers and I keep getting the same or similar errors:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The PowerScript

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://10.10.62.200/")
$link = @($ie.Document.getElementsByTagName('A')) | Where-Object {$_.href -match "`#javascript:powerOn()"}
$link.click()

Additional info & background

This is the link I am trying to click which is calling a javascript function:

<a href="javascript:powerOn();">
   <img src="./img/power_on_g.png" width="75" height="32" border="0" name="power_on"></a>

This code is actually in an iframe on a very simple webpage. It is the control panel for an NEC NC1200C projector for anyone else working on this same system. I am trying to write a script to pull up the web interface of the projector and turn it on automatically. I will include all of the code that makes up the pages in question below.

This is the HTML of the page being called: HAVING TROUBLE INSERTING CODE EVEN WITH CODE MARKUP

Links to similar questions:

Click a hyperlink using powershell

Powershell click on javascript link

How to click on that security hyperlink powershell

2
  • 1
    You're not waiting for IE to load the page before you look for the link. getElementsByTagName is finding nothing because the page hasn't loaded, so you try to click something which isn't a link. You need to include the ie.busy or ie.readystate -ne 4 from your linked posts. You may need to do more to work with an iFrame. I'd be watching with Firebug or Chrome dev tools or IE Dev tools or Fiddler2 proxy to see what the JavaScript actually sends, and see if I could send that directly with Invoke-WebRequest, and without using IE at all. Commented May 30, 2015 at 1:12
  • Afterthought - because it's PowerShell, you can type those things into the shell and control IE in realtime. Do $ie = new-object -com "InternetExplorer.Application" and then do $ie.Visible = $true and it will appear as a window, still being controlled by PowerShell. Then you can explore what $ie.Document,blahblah returns interactively, much easier and more fun than writing the script and guessing, finding it doesn't work, not knowing why. Commented May 30, 2015 at 1:21

1 Answer 1

2

Well, i finally make it works in Lab.

Assuming this sample HTML file :

<html>
<body>
<script type="text/javascript">
function powerOn(){
  document.location.href="http://google.com";
}
</script>
<a href="javascript:onclick=powerOn();"><img src="http://www.google.fr/url?source=imglanding&ct=img&q=http://www.picturesnew.com/media/images/e4454c8df9.png&sa=X&ei=lBhpVazqG6a07ga6l4JA&ved=0CAkQ8wc&usg=AFQjCNFnfXIWemeXiHvyqvfHMmgxOFcJFg" width="75" height="32" border="0" name="power_on"></a>
</body>
</html>

Then this Powershell script :

$ie = new-object -com internetexplorer.application
$ie.visible=$true
$ie.navigate('http://192.168.108.130')
$link=$ie.Document.getElementsByTagName('A') | Where-Object {$_.href -eq "javascript:onclick=powerOn();"}
$link.click()

From there, when i run my Powershell script i am successfully redirected to Google just like if i was clicking on the image.


I think your main problem has to do with the -match expression you used. Not sure why...but i've used -eq instead.

I've also add the onClick event in href directive which seems to be required to successfully call $link.click().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.