【发布时间】:2019-12-19 01:33:56
【问题描述】:
我正在尝试使用 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";
但是这给了我错误
You cannot call a method on a null-valued expression. At line:5 char: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()
提前谢谢你!
我正在使用:
- Windows 10 企业版(所以我想我的公司可能屏蔽了一些功能,我会与我的 IT 部门核实)
- Powershell 版本为 5.1
- Internet Explorer 版本 11.885
编辑: 正如评论中所建议的,我尝试使用 VBA 来执行与我的 powershell 脚本相同的操作,并且在 Document 对象 Method 'Document' of object 'IWebBrowser2' failed' 上遇到了相同的错误。这意味着我无法正确访问我的 Internet Explorer 应用程序。在这种情况下,我认为我应该等待我的 IT 部门的最终答复,然后再添加更多内容。
所以在我使用 Navigate 之前,我可以访问该对象及其所有属性。然后,即使Get-Member 也会出现错误,例如:
Get-Member : The following exception occurred while retrieving the string representation for property "Application" : "The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"
【问题讨论】:
-
我已经多次在 IE 中看到这种行为,但无法跨设备复制它。如果您需要自动化引擎,我不建议您使用 IE COM 对象,而是选择 selenium 之类的东西。
-
@TheIncorrigible1 谢谢,我去看看
-
@TheIncorrigible1 Selenium 看起来很有趣,但是我更喜欢不需要安装的东西,所以这就是我选择 PowerShell 的原因。不过谢谢你的提示!
-
不应该反对使用模块,因为它是软件开发过程的正常部分。您可以随时将 Selenium dll 与您的脚本捆绑使用。
-
@jrider 请不要假设你知道谁投了反对票。 OP 可能没有。
标签: powershell internet-explorer