2017-05-04 67 views
1

我想使用powershell訪問Jtrac網頁。我能夠登錄,但我無法訪問搜索按鈕哪個href鏈接。powershell IE自動化href問題

$Url = 「http://kbserver/workflow/app/login」 
enter code here`$Username=」XXXXX」 
enter code here`$Password=」XXXXX」 
$IE = New-Object -com internetexplorer.application; 
$IE.visible = $true; 
$IE.navigate($Url); 
while ($IE.Busy -eq $true) 
{ 
Start-Sleep -Milliseconds 2000; 
} 
$Login = $IE.document.getElementById("loginName3").value = "$Username" 
$Login = $IE.Document.getElementById(「password12」).value= "$Password" 
$Login = $IE.Document.getElementsByTagName("input") | where-object {$_.type -eq "submit"} 
$Login.click(); 
while ($IE.Busy -eq $true) 
{ 
Start-Sleep -Milliseconds 5000; 
} 
$Login = $IE.Document.getElementsByTagName("a") | where {$_.href -eq "'?wicket:interface=:2:table:dashboardRows:3:dashboardRow:search::ILinkListen  er::'"} 
$Login.click(); 

錯誤,我得到是

你不能調用一個空值表達式的方法。 在C:\ Users \ Dinesh \ Webbb.ps1:20 char:13 + $ Login.click < < < <(); + CategoryInfo:InvalidOperation:(點擊:字符串)[],RuntimeException的 + FullyQualifiedErrorId:InvokeMethodOnNull

<a href="?wicket:interface=:2:table:dashboardRows:3:dashboardRow:search::ILinkListener::"> 
 
<img title="SEARCH" src="../resources/search.gif"> </a>

回答

0

看起來你有19行,ILinkListen er::一個錯字在結束你的而不是ILinkListener::。哪裏找不到匹配的東西,所以它返回null,這就是你如何得到你的空值$ Login對象。

編輯:你的問題仍然是第19行,你應該打印出$IE.Document.getElementsByTagName("a")並確認它實際上正在返回你所期望的,因爲你的where過濾器不匹配任何內容並返回$ null到$ Login。編輯2:去了一個網站,抓住了所有的hrefs並看了看,powershell從hrefs中刪除了「」。您可以改爲$IE.Document.getElementsByTagName("a") | where {$_.href -eq "?wicket:interface=:2:table:dashboardRows:3:dashboardRow:search::ILinkListener::"}

編輯3:您的第二個問題可以用正則表達式使用-match運算符來解決,而不是-eq(如果您檢查的項目是dyanmic,-eq不會真正起作用)。

像這樣:

$IE.Document.getElementsByTagName("a") | where {$_.href -match "\?wicket:interface=:\d"}

此正則表達式將返回任何containts 「檢票:界面=:數字」。 一位數字是最好的選擇,因爲它會在刷新時保持增長,假設頁面上沒有其他內容匹配,這應該是好的。

+0

對不起。當我粘貼在這裏的錯字錯誤... –

+0

我已根據您的信息更新了我的答案。 – Phil

+0

仍然沒有運氣,得到同樣的錯誤。 –