使用Powershell自动登录网站

iri*_*ish 5 powershell scripting

在此处输入图片说明运行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`
Run Code Online (Sandbox Code Playgroud)

我的剧本:

$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()
Run Code Online (Sandbox Code Playgroud)

我似乎无法理解这里的问题,我已经研究了stackoverflow中的其他示例,但仍然找不到问题。

在另一个python脚本示例中,相同的id可以正常工作。

这是截图 编辑:提供元素的屏幕截图

JPB*_*anc 6

问题来自于这样的事实'ysi_email',即在加载的文档的 DOM 中找不到ID'ysi_password'和 的对象。'ysi_btn_login'https://website.com/login

为了解决您的问题,请在 Chrome、Firerfox 或 Explorer 中加载您的文档,并激活开发人员工具(按 F12)并检查您想要查找的对象。

在此输入图像描述


根据您的评论,这是一个可行的解决方案:

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

$username="test@toto.fr"

$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()
Run Code Online (Sandbox Code Playgroud)

在这里你可以看到我的结果。

在此输入图像描述


在此输入图像描述