【问题标题】:Internet Explorer object doesn't have Document childInternet Explorer 对象没有 Document 子对象
【发布时间】: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


【解决方案1】:

看起来正在发生的事情是在您调用 $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')

【讨论】:

  • @JackA 当我运行它时,我能够访问$inputbox 变量。
  • 我会尝试重新启动一切,看看问题是否得到解决。正如在原始帖子的编辑中提到的,我会询问我的 IT 部门。
【解决方案2】:

这是在这里工作:

$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"

您必须等到浏览器加载完所有对象。

【讨论】:

    【解决方案3】:

    所以问题其实很简单,简单到我都没有想到……我必须以管理员身份运行程序,然后它才能工作。

    感谢大家的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2022-12-21
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      相关资源
      最近更新 更多