2016-11-16 82 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