在Powershell中无法在此对象上找到属性'值'

问题描述 投票:2回答:2

我想在脚本中设置“用户名”的以下HTML。

<input id="user_name" value="" autocomplete="off" type="text" name="user_name">

这是我为实现此目的而编写的powershell代码:

$ie = new-object -com InternetExplorer.Application
$ie.Visible = $true
$ie.Navigate("https://abcdnow.com/navpage.do")

while ($ie.Busy) {start-sleep -s 3}

$ie.Document.getElementByID("user_name").value = "Username"
$ie.Document.getElementByID("user_password").value = "Password"

这是我收到的错误:

The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At D:\servicenow.ps1:7 char:1
+ $ie.Document.getElementByID("user_name").value = "Username"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
javascript regex powershell getelementbyid
2个回答
1
投票

这是因为您的控件在框架内。您必须这样做:

$ie.Document.getElementById("gsft_main").contentWindow.document.getElementByID("user_name").value = "UserName"

0
投票

如果使用标记为解决方案的方法后出现此错误怎么办?

您不能在空值表达式上调用方法。在C:\ Users \ USER \ Desktop \ weblogin.ps1:8 char:1+ $ ie.Document.getElementById(“ gsft_main”)。contentWindow.document.getEl ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~+ CategoryInfo:InvalidOperation:(:) [],RuntimeException+ FullyQualifiedErrorId:InvokeMethodOnNull

© www.soinside.com 2019 - 2024. All rights reserved.