我想在脚本中设置“用户名”的以下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
这是因为您的控件在框架内。您必须这样做:
$ie.Document.getElementById("gsft_main").contentWindow.document.getElementByID("user_name").value = "UserName"
如果使用标记为解决方案的方法后出现此错误怎么办?
您不能在空值表达式上调用方法。在C:\ Users \ USER \ Desktop \ weblogin.ps1:8 char:1+ $ ie.Document.getElementById(“ gsft_main”)。contentWindow.document.getEl ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~+ CategoryInfo:InvalidOperation:(:) [],RuntimeException+ FullyQualifiedErrorId:InvokeMethodOnNull