powershell $ ie.Document.getElementById(" username")。value ="我的用户名"错误

时间:2017-05-03 17:29:50

标签: powershell internet-explorer

我想使用powershell自动化登录表单:

<form method="post" class="login-form" name="loginForm" action="/Service/WebObjects/Service.woa/wo/0.15.7.3.1">
<label>Username</label>
<input maxlength="128" style="width: 240px;" type="text" name="username" />
<label>Password</label>
<input maxlength="64" style="width: 240px;" type="password" name="password" />
<input class="login-check-input" type="checkbox" name="rememberCheckBox" value="15.7.3.1.15" />
<label class="login-check-label" for="flag_stay_logged_in">Remember login</label><br/>
<noscript>
You need a JavaScript enabled browser to use this application.
</noscript>
<script language="javascript" type="text/javascript">
document.write('<div class="buttonBar"><input alt="Login" class="button" type="submit" value="Login" name="ButtonLogin" /></div>');
</script>
</form>

$ie=New-Object -comobject InternetExplorer.Application  
$ie.visible=$true  
$ie.ParsedHtml
$ie.Navigate("https://livetime/Service/WebObjects/Service")  
while($ie.busy){Start-Sleep 1}  

$ie.Document.getElementById("username").value="my username "  
$ie.Document.getElementById("password").value="my password " 
$ie.Document.getElementById('ButtonLogin').click()  

运行此脚本后,我收到以下错误:

The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At line:7 char:1
+ $ie.Document.getElementById("username").value="mt 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 line:8 char:1
+ $ie.Document.getElementById("password").value="my password "
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

我发现互联网上的例子表明这个脚本应该有用。

我能/应该检查一下(不同/旧版本或设置)?

1 个答案:

答案 0 :(得分:0)

问题在于没有为要获取的元素定义 id (定义了名称,但这不一样!)。对于您引用的示例,您会看到还没有定义 id 属性。 这意味着您必须以另一种方式获取相关元素,一种可能的方法是使用 getElementsByTagName 来获取输入并相应地枚举它们:

$ie=New-Object -comobject InternetExplorer.Application  
$ie.visible=$true  
$ie.ParsedHtml
$ie.Navigate("https://livetime/Service/WebObjects/Service")  
while($ie.busy){Start-Sleep 1}
$document = $ie.Document
$form =  $document.forms[0]
$inputs = $form.getElementsByTagName("input")
($inputs | where {$_.name -eq "username"}).value = "my username"
($inputs | where {$_.name -eq "Password"}).value = "my password"

我已经确认这适用于我的简化测试页面(也没有定义输入的ID):

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <form method="post" class="login-form" name="loginForm">
        <label>Username</label>
        <input maxlength="128" style="width: 240px;" type="text" name="username" />
        <label>Password</label>
        <input maxlength="64" style="width: 240px;" type="password" name="password" />
        </form>
    </body>
</html>

如果您仍然无法使用特定网页(我无法从此处访问),我建议您自己构建一个简化的HTML页面,并通过(便携式)网络服务器提供(例如http://www.usbwebserver.net)。所以我们确实在同一页上......