Powershell click on javascript link - Stack Overflow

Here is hyperlink code<a href="javascript:void(1)" onclick="server_playOn(17,2, '

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 -object InternetExplorer.Application
$ie.visible=$true
$ie.FullScreen=$true
$ie.navigate(".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";
$ie.document.getElementById("username").value = "$username"
$ie.document.getElementById("pass").value = "$password"
$ie.document.getElementById("frmLogin").submit()
start-sleep 5
$ie.navigate(".shtml")
##need to add click code here

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 -object InternetExplorer.Application
$ie.visible=$true
$ie.FullScreen=$true
$ie.navigate("http://www.example./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.";
$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./en/link5.shtml")
##need to add click code here
Share Improve this question asked Jun 13, 2013 at 0:07 MowgliMowgli 3,51222 gold badges65 silver badges90 bronze badges 3
  • 1 Just a thought, but if you don't mind using a 3rd party library to acplish this, then you could use something like the Selenium PowerShell eXtensions (sepsx.codeplex.). Selenium has a much better API for interacting with HTML Elements. – Roberto Hernandez 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. – Mowgli Commented Jun 13, 2013 at 2:08
  • @JPBlanc See the answer which is marked as accepted answer. – Mowgli Commented Jun 13, 2013 at 14:44
Add a ment  | 

2 Answers 2

Reset to default 3

Add these lines at the end of the script:

$link = @($ie.Document.getElementsByTagName('A')) | Where-Object {$_.innerText -eq 'Example'}
$link.click()

Another way could be

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745003948a4605661.html

相关推荐

  • Powershell click on javascript link - Stack Overflow

    Here is hyperlink code<a href="javascript:void(1)" onclick="server_playOn(17,2, '

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信