2013-03-06 38 views
2

我想寫一個基本的PowerShell腳本登錄到Gmail,但有一些問題。代碼如下。我已經評論了一些東西來縮小範圍。PowerShell網站登錄不工作

$url = "http://gmail.com" 
$username="myusername" 
$password= "123456" 
$ie = New-Object -com InternetExplorer.Application 


$ie.visible = $true; 
$ie.navigate($url); 
while ($ie.Busy) 
{ 
Start-Sleep -m 10000; 
} 

$counter = 0 
while (($counter -lt 10) -and ($ie.document -eq $null)) {Start-Sleep 1; $counter++} 

$ie.document -eq $null 
$ie.Document.getElementByID('email').value=$username 
#$ie.Document.getElementByID("Password").value=$password 
#$ie.Document.getElementById("signin").Click(); 

當我運行powershell腳本時,出現以下錯誤。

You cannot call a method on a null-valued expression. 
At J:\scripts\gmail.ps1:21 char:1 
+ $ie.Document.getElementByID('email').value=$username 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
+ FullyQualifiedErrorId : InvokeMethodOnNull 

奇怪的是,這是昨天和今天完全正常工作完全沒有工作。我正在使用IE9。

任何幫助非常感謝。

+0

我剛剛測試了你的代碼,它在這裏工作。然而,在我的情況下,「密碼」字段被稱爲「Passwd」(也用gmail進行測試)。你檢查這些字段名是否是靜態的嗎? – Poorkenny 2013-03-06 10:20:26

+0

你確定你給頁面足夠的時間來加載?你有一個計數器到10,睡1秒,你可能不得不增加或刪除計數器,使其無限期,直到$ ie.document不等於$ null – 2013-03-06 10:29:01

+0

現在得到以下錯誤:方法調用失敗,因爲[系統.__ ComObject]不包含名爲'getElementByID'的方法。 在C:\ Users \ techjlw \ Documents \ Untitled1.ps1:18 char:28 + $ ie.Document.getElementByID <<<<('email')。value = $ username + CategoryInfo:InvalidOperation:(getElementByID:字符串)[],RuntimeException + FullyQualifiedErrorId:MethodNotFound – 2013-03-06 10:33:45

回答

0

我覺得你在這裏犯了一個錯誤:

$ie.Document.getElementByID("Password").value=$password 

的ID應該passwd文件,沒有密碼。因此,請將該行更換爲:

$ie.Document.getElementByID("Passwd").value=$password 

適用於我。

+0

我甚至在我到達那裏之前得到錯誤。這一點被註釋掉了。我在登錄字段中出現錯誤。 – 2013-03-06 10:15:21

+0

奇怪。我試過你的腳本,它適用於我(在進行建議更換時)。 – 2013-03-06 10:17:42

+0

恩,很抱歉問一個愚蠢的問題(但它發生在我身上......)。在嘗試使用腳本之前,你有沒有註銷?如果您有Gmail的「保持登錄狀態」選項,您的腳本將永遠不會看到登錄頁面... – 2013-03-06 10:21:48

4

PowerShell IE9 ComObject has all null properties after navigating to webpage給了我這個問題的答案。必須以管理員身份運行我的腳本,因爲IE保護模式會阻止創建對象。要麼關閉IE保護模式,要麼以管理員身份運行腳本以使其工作。

+0

管理員甚至沒有幫助我 - 必須禁用https保護區的要求,並手動添加網站。 – Josiah 2016-07-05 18:43:37