首页 文章

使用Powershell进行IE登录自动化

提问于
浏览
0

我正在尝试使用以下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()

以下是我的网址的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>

我收到以下错误消息:

您无法在空值表达式上调用方法 . 在行:14 char:1 $ Link.click()~~~~~~~~~~~~~ CategoryInfo:InvalidOperation:(:) [],RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull

1 回答

  • 0

    问题是您正在尝试使用函数 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>
    

相关问题