1

Here is hyperlink code

<a href="javascript:void(1)" onclick="server_playOn(17,2, 'est', this);">
                     Example  
                  </a>

Here is my current powershell code.

Get-Process iexplore | Foreach-Object { $_.CloseMainWindow() }
$username = "user" 
$password = "pass"
$ie = New-Object -comobject InternetExplorer.Application
$ie.visible=$true
$ie.FullScreen=$true
$ie.navigate("http://www.example.com/en/index.shtml")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
trap [Exception] 
{ 
    # This will happen if you're already logged in 
    if($_.Exception.Message -eq "Property 'value' cannot be found on this object; make sure it exists and is settable." -Or $_.Exception.Message -eq "You cannot call a method on a null-valued expression.") 
    { 
        # Try and skip this error 
        continue; } 
    else { 
        # Fail for other Exceptions 
    Write-Host -ForegroundColor Red $_.Exception.Message; } 
    }

##if not logged in
Write-Host -ForegroundColor Green "Logging into example.com";
$ie.document.getElementById("username").value = "$username"
$ie.document.getElementById("pass").value = "$password"
$ie.document.getElementById("frmLogin").submit()
start-sleep 5
$ie.navigate("http://www.example.com/en/link5.shtml")
##need to add click code here
3
  • 1
    Just a thought, but if you don't mind using a 3rd party library to accomplish this, then you could use something like the Selenium PowerShell eXtensions (sepsx.codeplex.com). Selenium has a much better API for interacting with HTML Elements. Commented Jun 13, 2013 at 0:20
  • Thanks robert. I'll keep in mind, if since I don't know how to use it but will give it try. Commented Jun 13, 2013 at 2:08
  • @JPBlanc See the answer which is marked as accepted answer. Commented Jun 13, 2013 at 14:44

2 Answers 2

3

Add these lines at the end of the script:

$link = @($ie.Document.getElementsByTagName('A')) | Where-Object {$_.innerText -eq 'Example'}
$link.click()
Sign up to request clarification or add additional context in comments.

8 Comments

Awesome man. I was hoping you would answer my question and you did. I read many for your answers they are very helpful.
Can you help me bit. I am facing bit of a problem. How can I make it work if the link I need to click. it is in a iframe. is there a workaround for that ? thanks
IIRC you'll need to navigate to the iframe page, get the link and click it.
Shay, please check this img of source code. looks like it is not iframe for this html page. powershell is not clicking the link please help. farm8.staticflickr.com/7333/9034029697_2a2b56c447_o.png
You need to click the link that says: 'Life OK' ?
|
0

Another way could be

$ie.Document.parentWindow.execScript($Javascript, "javascript")

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.