2016-11-16 78 views
2

enter image description here我收到以下錯誤消息,當我運行我的PowerShell腳本自動登錄到網站上使用PowerShell

The property 'value' cannot be found on this object. Verify that the property exists and can be set. 
At C:\Test.ps1:17 char:1 
+ $usernamefield.value = $username 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : PropertyAssignmentException 

The property 'value' cannot be found on this object. Verify that the property exists and can be set. 
At C:\Test.ps1:20 char:1 
+ $passwordfield.value = $password 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : PropertyAssignmentException 

Method invocation failed because [System.DBNull] does not contain a method named 'click'. 
At C:\Test.ps1:23 char:1 
+ $Link.click() 
+ ~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : MethodNotFound` 

我的腳本:

$ie = New-Object -ComObject 'internetExplorer.Application' 
$ie.Visible= $true # Make it visible 

$usernmae="test" 

$password="test1" 

$ie.Navigate("https://website.com/login") 

While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;} 

$usernamefield = $ie.document.getElementByID('ysi_email') 
$usernamefield.value = $username 

$passwordfield = $ie.document.getElementByID('ysi_password') 
$passwordfield.value = $password 

$Link = $ie.document.getElementByID('ysi_btn_login') 
$Link.click() 

我似乎無法理解的問題在這裏,我已經看到了其他的例子,但我仍然無法找到問題。

在Python腳本的另一個例子中,相同的id可以正常工作。

下面是截圖Edit: Providing screenshot of the elements

+0

如果你這樣設置,同樣的問題? $ ie.document.getElementByID('ysi_email')。value = $ username – Esperento57

+0

你的名字是'ysi_email'嗎? – Esperento57

+0

是的,請看附件截圖。這個ID適用於python和chrome,但沒有使用powershell和IE? – irish

回答

2

的問題來自於一個事實,即標識'ysi_email''ysi_password''ysi_btn_login'的對象在https://website.com/login加載文檔的DOM都沒有找到。

要解決您的問題,請在Chrome或Firerfox或Explorer中使用已激活的開發工具(按F12)和檢查查找要查找的對象。

enter image description here


這裏是一個可行的解決方案,根據您的意見:

$ie = New-Object -ComObject 'internetExplorer.Application' 
$ie.Visible= $true # Make it visible 

$username="[email protected]" 

$password="test1" 

$ie.Navigate("https://To your detailled URL") 

While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;} 

$usernamefield = $ie.document.getElementByID('ysi_email') 
$usernamefield.value = "$username" 

$passwordfield = $ie.document.getElementByID('ysi_password') 
$passwordfield.value = "$password" 

$Link = $ie.document.getElementByID('ysi_btn_login') 
$Link.click() 

$ie.Quit() 

在這裏你可以看到的結果對我來說。

enter image description here


enter image description here

+0

對不起,只是試圖瞭解這一點,在IE瀏覽器和Chrome瀏覽器的DOM會有所不同嗎? python腳本基本上是調用chrome,而在powershell中,我打電話給IE? – irish

+0

謝謝。我明白,這就是爲什麼我在這裏尋找一些幫助。 – irish

+0

@irish,我修改我的答案,只要你問我,我會從答案和評論中刪除你的網址。 – JPBlanc