我正试着用PowerShell填写一份表格。所以我只是想这么做:
$ieObject = New-Object -ComObject 'InternetExplorer.Application';
$ieObject.Visible = $true;;
$ieObject.Navigate('https://www.randomizer.org/');
$currentDocument = $ieObject.Document;
$inputbox = $currentDocument.getElementByID('randSets');
$inputbox.value = "My Value"; 然而,这给了我错误。
不能对空值表达式调用方法。行:5字符:1+ $inputbox = $currentDocument.getElementByID("randSets");
我不知道为什么,但是我的ieObject没有所有的属性(当我应该拥有大约50个属性时,我只有9个属性)和方法。当我使用Get-Member时,我看不到Document在里面,所以这是正常的还是我做错了什么?
PS C:\Users\n> $ieObject | Get-Member
TypeName: System.__ComObject
Name MemberType Definition
---- ---------- ----------
CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type re...
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetLifetimeService Method System.Object GetLifetimeService()
GetType Method type GetType()
InitializeLifetimeService Method System.Object InitializeLifetimeService()
ToString Method string ToString() 提前谢谢你!
我正在使用:
编辑:,正如注释中所建议的,我尝试使用VBA来做与我的powershell脚本相同的事情,并且在Document对象Method 'Document' of object 'IWebBrowser2' failed'上得到了相同的错误。这意味着我无法正确访问Internet应用程序。在这种情况下,我认为我应该等待我的IT部门的最终答案,然后再添加更多。
因此,我可以访问对象和它的每个属性,直到我使用导航。那么,即使对于Get-Member,我也会得到错误,比如:
Get成员:在检索属性"Application“的字符串表示时发生了以下异常:”RPC服务器不可用(来自HRESULT: 0x800706BA的异常)“
发布于 2019-08-19 14:14:41
所以问题其实很简单,很简单,我没想过.我必须以管理员的身份运行程序,然后它才能工作。
谢谢大家的帮助!
发布于 2019-08-12 13:12:35
看起来正在发生的情况是,当您调用$currentDocument = $ieObject.Document;时,页面还没有加载,导致$currentDocument成为$null。
您应该能够通过使用.Busy修复它。
$ieObject = New-Object -ComObject 'InternetExplorer.Application'
$ieObject.Visible = $true
$ieObject.Navigate('https://www.randomizer.org/');
do{
Start-Sleep -Milliseconds 10
}while($ieObject.Busy)
$currentDocument = $ieObject.Document
$inputbox = $currentDocument.getElementByID('randSets')发布于 2019-08-12 15:19:29
这在这里起作用:
$ieObject = New-Object -Com InternetExplorer.Application
$ieObject.Visible = $true
[void]$ieObject.Navigate2('https://www.randomizer.org/')
while ($ieObject.busy) {
sleep -seconds 1
}
$currentDocument = $ieObject.Document
$inputbox = $currentDocument.getElementByID('randSets')
$inputbox.value = "My Value"你必须等到浏览器加载了所有的对象。
https://stackoverflow.com/questions/57461731
复制相似问题