2014-12-03 19 views
0

我倒是想點擊一個鏈接,至極從來就filterd使用PowerShellPowerShell的getElementByTagName( 「A」)返回NULL

下面是代碼:

$ie.Document.getElementByTagName("a")| foreach { 
if ($_.href -eq "name.pdf"){ 
$_.click() 
    } 
} 

當我運行該腳本我得到一個錯誤這樣的:

FullyQualifiedErrorId:InvokeMethodOnNull

但我不知道什麼是錯,這裏是HTML代碼:

<td><a href="javascript:NewWindow('name.pdf')">Name of the link</a> 

我將是任何形式的幫助表示感謝。

在此先感謝

這裏是整個PowerShell代碼:

$ie = new-object -com internetexplorer.application 
$ie.visible=$true 
$ie.navigate('url') 

while($ie.busy) { 
sleep 5 
} 
$ie.Document.getElementByTagName("a")| foreach { 
if ($_.href -eq "name.pdf"){ # or eq Name of the link 
$_.click() 
    } 
} 
+0

這可能是我的IE有問題?我正在使用IE 9 – user3320668 2014-12-03 09:14:39

回答

0

你做兩件錯誤的事情。

  1. 不GetElementByTagName但的getElementsByTagName
  2. EQ不工作使用像

    $ URL = 「https://stackoverflow.com/

$即=新對象-com internetexplorer.application

$ ie.visible = $ true

$ ie.navigate($網址)

while($ie.busy) { 
sleep 5 
} 

$links = $ie.Document.GetElementsByTagName("a") 
foreach ($a in $links){ 
    $href = $a.href 
    if ($href -like '*questions*') 
    { 
     print "Found Link : $href Clicking" 
     $a.click() 
     break 
    } 
}