【问题标题】:InternetExplorer.Application com object and windows 2012 in powershellInternetExplorer.Application com 对象和 powershell 中的 windows 2012
【发布时间】:2014-01-17 22:27:19
【问题描述】:

我正在尝试使用 Windows 2012 访问 Internet Explorer com 对象的文档。该代码在 Windows 2008 中运行良好,但一旦我尝试在 Windows 2012 上运行它(全新安装,在多个服务器上尝试) ,相同的代码停止工作。换句话说,$ie.document.documentHtml 返回为 null。

下面是代码:

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate2("http://www.example.com/") 
while($ie.busy) {start-sleep 1}
$ie.document.documentHtml.innerhtml

interexplorer com 对象在 windows 2012 中是否发生了变化?如果是,我如何在 windows 2012 中检索文档内容?

提前致谢

edit:添加了赏金以使事情变得更甜蜜。 Invoke-WebRequest 很好,但它只能在 Windows 2012 上运行,但我需要使用 Internet Explorer 并让它在 Windows 2008 和 Windows 2012 上运行。我在某处读过安装 microsoft office 解决了这个问题。这也不是一种选择。

edit2:由于我需要在多个 Windows 服务器(2008 和 2012)上远程调用脚本,我不希望手动复制文件

【问题讨论】:

  • “停止工作”是什么意思?您收到错误消息吗?你期待什么结果,你收到什么结果?它在哪条线上失败了?
  • 我的意思是代码不起作用。换句话说,$ie.document.innerhtml 是空的。有趣的是,我可以使用 $ie.visible=$true 使浏览器可见,它显示浏览器已导航到正确的页面,但我无法访问实际页面内容
  • 通过网络搜索,似乎我不是唯一遇到此问题的人,而且似乎在某些情况下,安装 Office 2010 可以解决问题 - 这不是我的选择跨度>

标签: powershell com internet-explorer-10 createobject


【解决方案1】:

获取任何安装了 Office 的 PC,并将 Microsoft.mshtml.dll 复制到您的脚本位置。 c:\program files (x86)\Microsoft.net\primary interop assembly\Microsoft.mshtml.dll

add-Type -Path Microsoft.mshtml.dll

脚本有效。

【讨论】:

    【解决方案2】:

    这是一个已知的错误:http://connect.microsoft.com/PowerShell/feedback/details/764756/powershell-v3-internetexplorer-application-issue

    解决方法的摘录:

    所以,这里有一个解决方法:

    1. 从安装位置复制Microsoft.html.dll(例如:从 C:\Program Files(x86)\Microsoft.NET\Primary Interop Assemblies 到脚本所在的位置(可以是网络驱动器)
    2. 使用Load-Assembly.ps1 脚本(下面提供的代码:http://sdrv.ms/U6j7Wn)将程序集类型加载到内存中 例如:.\Load-Assembly.ps1 -Path .\microsoft.mshtml.dll

    然后像往常一样继续创建 IE 对象等。警告:在处理 write() 和 writeln() 方法时,请使用向后兼容的方法:IHTMLDocument2_write() 和 IHTMLDocument2_writeln()。

    【讨论】:

    • 我确实投了赞成票,但由于我在多台机器上远程使用 powershell,所以不实用。谢谢
    • 虽然我更喜欢不涉及在多个文件上复制 dll 的解决方案,但这个答案最接近,赏金即将到期 - 谢谢
    【解决方案3】:
        $ie.document.documentHtml.innerhtml
    

    更大的问题是这怎么可能奏效。 Document 属性返回对IHTMLDocument interface 的引用,它没有“documentHtml”属性。当您像在此代码中所做的那样使用后期绑定时,您可能会得到什么从来都不是很清楚。 DHTML 编辑控件支持一个旧的 documentHtml 属性,该属性已被牢固地放置在the pasture 中。诚然,这是一个疯狂的猜测。

    无论如何,正确的语法是使用body 属性:

      $ie = new-object -com "InternetExplorer.Application"
      $ie.navigate2("http://www.example.com/") 
      while($ie.busy) {start-sleep 1}
      $txt = $ie.document.body.innerhtml
      Write-Output $txt
    

    如果您仍然有问题,Powershell 确实无法诊断空引用,然后尝试在机器上运行此 C# 代码。应该给你一个更好的信息:

    using System;
    
    class Program {
        static void Main(string[] args) {
            try {
                var comType = Type.GetTypeFromProgID("InternetExplorer.Application");
                dynamic browser = Activator.CreateInstance(comType);
                browser.Navigate2("http://example.com");
                while (browser.Busy) System.Threading.Thread.Sleep(1);
                dynamic doc = browser.Document;
                Console.WriteLine(doc.Body.InnerHtml);
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }
    }
    

    【讨论】:

    • 在我的 windows 2012 上,$ie.document.body 和 $ie.document.body.innerhtml 都不可用。不过谢谢
    • 这非常奇怪,IE 对象模型已经存在了很长时间,在 2012 年并没有什么不同。在该机器上启动 Regedit.exe 并导航到 HKCR\InternetExplorer.Application。引用您在此处看到的 CLSID 键值。并从其 Help + About 对话框中引用 IE 版本。
    • 感谢您的帮助。 IE 版本 10.0.9200.16384,clsid 为 {0002DF01-0000-0000-C000-000000000046}
    • 有趣的是 $ie.document 不为空,当我输入 $ie.document 时,它说它是 System.__ComObject
    • 这完全正常。 Powershell 在没有诊断的情况下吞下空引用的习惯使得调试问题变得非常困难。即将放弃这个,用可以给你更好诊断的代码更新帖子。
    【解决方案4】:

    据我所知,在 Windows Server 2012 上获取页面的完整 html:

    $ie.document.documentElement.outerhtml
    

    documentElement 上还有一个 innerhtml 属性,它会剥离根 <html> 元素。

    当然,如果您只想获取原始标记,请考虑使用Invoke-WebRequest

    $doc = Invoke-WebRequest 'http://www.example.com'
    $doc.Content
    

    【讨论】:

    • 我打算写 $ie.document.documentElement.innerhtml - 它在 2012 年是空的。我将编辑我的帖子。 $ie.document 是 System.__ComObject 但键入 $ie.document.documentElement 不会返回任何内容。关于 Invoke-WebRequest 的信息很有趣,所以我会投票,但不幸的是,我需要使用 Internet Explorer。
    猜你喜欢
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多