使用Powershell的IE登录自动化



我正在尝试使用以下Powershell脚本登录到IE:

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
$password="password"
$ie.Navigate("URL")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$passwordfield = $ie.document.getElementByID('password')
$passwordfield.value = "$password"
$Link = $ie.document.getElementByID('Login')
$Link.click()

下面是我的URL的HTML代码:

<form action="index.cfm?event=dashboard:config.index" method="post">
<h4>Password</h4>
<p>
    <input type="password" name="password" value="" size="20">&nbsp;
    <input type="submit" value="Login">
</p>

我收到以下错误消息:

不能对 null 值表达式调用方法。 行:14 字符:1 + $Link.click(( + ~~~~~~~~~~~~~ + 类别信息 : 无效操作: (:) [], 运行时异常 + FullQualifiedErrorId : InvokeMethodOnNull

问题是您正在尝试使用函数 getElementByID 访问代码中的元素。但是,示例代码中的密码输入和提交按钮都没有 ID。因此,如果提供的示例HTML代码是正确的,则您的脚本应该在$passwordfield.value = "$password"上崩溃。

您可以使用其他方法来获取正确的字段,查找GetElementsByName,或者您可以更新 HTML 代码以使用 ID:

<p>
    <input type="password" id="password" name="password" value="" size="20">&nbsp;
    <input type="submit" id="submit" value="Login">
</p>

最新更新