Powershell System .__ ComObject.document属性在IE 9下不再起作用 [英] Powershell System.__ComObject.document property no longer works under IE 9

查看:150
本文介绍了Powershell System .__ ComObject.document属性在IE 9下不再起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写一个脚本,该脚本使用Web浏览器界面运行一些服务器功能.我使用Internet Explorer 8在Windows 7上编写了脚本,并且运行良好. 一旦将其移动到运行带有Internet Explorer 9的Windows 2008的生产服务器,它就会崩溃.最终找到了它的故障点,但是我对如何解决它有些困惑. 这是会引起问题的代码:

I've been writing up a script that runs some server functions using a web-browser interface. I coded up the script on Windows 7 with Internet explorer 8 and it works fine. As soon as I move it to the production server running Windows 2008 with Internet Explorer 9, it breaks. Finally traced it the point of failure, but I'm a bit stumped how to fix it. Here's the code that will cause an issue:

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://www.google.com")
$ie.visible = $True
$doc = $ie.document
$Object1 = $doc.getElementByID("pocs")

这会弹出IE窗口,它应该能够按ID搜索元素.麻烦的是,现在我得到了错误

This pops up an IE windows, and it should be able to search elements by ID. Trouble is, now I get the error

找不到" getElementById的重载,并且参数计数为: "1"."

"Cannot find an overload for "getElementById" and the argument count: "1"."

关于此错误,我发现得很少.实际的问题实际上是变量$ doc.如果在IE 9上执行"$ doc | get-member",我将得到:

I can find very very little on this error. The actual issue is actually the variable $doc. If I do a "$doc | get-member" on IE 9 I get:

TypeName: System.__ComObject#{c59c6b12-f6c1-11cf-8835-00a0c911e8b2}

但是在IE 8下,我得到了:

But under IE 8 I get:

  TypeName: mshtml.HTMLDocumentClass

因此,基本上,当我调用$ ie.document时,IE 9/Windows 2008无法加载Web文档内容.我已经尝试将IE9设置为兼容模式,但是那里没有运气.

So, basically, IE 9 / Windows 2008 is failing to load the web document contents when I call $ie.document. I've tried setting IE9 to compatibility mode, but no luck there.

$ ie.document | get-member实际上确实显示了以下方法:"getElementById Method Variant getElementById()",所以它位于其中,但没有文档可供解析.

The $ie.document | get-member does actually show the method of : "getElementById Method Variant getElementById () " so it's in there, but there's no document for it to parse.

任何想法都将不胜感激.

Any ideas would be greatly appreciated.

推荐答案

我同样感到惊讶的是,鉴于该问题在各种技术论坛上的寿命很长,这一问题尚未得到解决.但是,我认为我已经找到了解决方案,尽管这将由Microsoft IE团队来解决.

I am equally astonished this has not been fixed yet given the longevity of the issue on various technical forums. However I think I have found the solution, though it would be up to the Microsoft IE team to address at some point.

就像所有引用此问题的线程一样,我在getElementById方法上遇到了相同的问题,该方法没有对几台测试机(一个Windows 2008R2 Enterprise 64位和一个Windows 7 32位)进行其他更改,我可以使用相同的脚本来工作.

Like all the threads referenced that have looked into this I have suffered the same issue with the getElementById method, which with no other changes to a couple of test machines (one Windows 2008R2 Enterprise 64bit and one Windows 7 32bit), I can get the same script to work.

我不喜欢的临时解决方案:

Workarounds that worked as temp solution that I didn't like:

  1. 使用IE11中的开发控制台,将文档模式切换为8(9、10、11,Edge(默认)不起作用)-我的自动化脚本可立即工作.无需更改IE可信站点,区域安全性,保护模式,PowerShell会话特权).显然,这只是IE11安装中的一个组件问题
  2. 在安装Office 2013时,无需运行或授权它,即可立即运行相同的脚本,而无需更改IE11的文档模式.显然,Office确实安装/注册了可解决问题的工具(如Rhys Edwards所说)

因此,我开始通过以下方式缩小Office为启用IE自动化所需的COM对象所做的工作:

So I set about narrowing down what Office does to enable the COM object required for IE automation by:

  1. 正在准备新的Virtual Windows 2008R2 Server,没有更新.在IE8下运行测试脚本-没问题.

  1. Preparing a new Virtual Windows 2008R2 Server , no updates. Ran test script under IE8 - no issues.

已升级到IE11.跑测试脚本-照常失败.

Upgraded to IE11. Ran test script - failed as usual.

拍摄虚拟机快照

使用Regshot记录注册表和文件系统

Used Regshot to do record the registry and file system

运行Office 2013 Pro_SP1安装,未更改默认选项 Office安装完成后,一次都没有运行Office 再次运行测试脚本-一切正常,使用getElementById的IE自动化使所有操作恢复正常

Ran the Office 2013 Pro_SP1 installation, no changes to default options When Office install completes, did not run office once (at all ever) Ran test script again - everything works, the IE automation with getElementById calls all back in operation

拍摄第二个VM快照

使用regshot进行了第二次扫描并分析了差异

Ran 2nd scan with regshot and analysed the differences

转储了我的$ie对象的属性,并且还注意到那里比运行Office安装之前要多得多.整个引用mshtml.dllHTMLDocument类-看起来应该

Dumped the properties of my $ie object and also noticed there is far more there than before running Office install. References mshtml.dll and HTMLDocument classes throughout - looks as it should

我从RegShot差异文件中看到,通过办公安装将MSHTML.dll添加并注册到GAC(版本7.0.3300.0)中

I can see from the RegShot difference file that MSHTML.dll is ADDED and registered in the GAC (version 7.0.3300.0) by the office installation

我接下来做的事情可能未完全获得批准,但:

What I did next may not be completely approved but:

  1. 我在"c:\program files(x86)\Microsoft.net\primary interop assemblies"文件夹中找到了microsoft.mshtml.dll并将其保存到VM之外的本地计算机桌面上

  1. I located the microsoft.mshtml.dll in the "c:\program files(x86)\Microsoft.net\primary interop assemblies" folder and saved it out of the VM to my local machine desktop

还原到办公室前2013年快照

reverted to the pre-office 2013 snapshot

microsoft.mshtml.dll复制到VM中并安装到GAC(请记住,这是仍位于.net 2上的2008R2服务器,在安装IE11之前或之后,我没有更新.net,只有办公室).我只需将文件拖动到资源管理器的c:\windows\assembly视图中即可安装到GAC.在更高版本的.Net中,您需要使用gacutil /l

copied the microsoft.mshtml.dll into the VM and installed to the GAC (remember this is a 2008R2 server still on .net 2, I didn't update .net prior to or after IE11 install, only office). I installed to the GAC simply by dragging the file into the c:\windows\assembly view in explorer. In later versions of .Net you need to use gacutil /l

测试了相同的脚本和BOOM,一切正常.无需更改任何IE设置或提升脚本权限或安装Office

Tested the same script and BOOM, it all works fine. No need to change any IE settings or elevate script privileges or install Office

所以总结一下.如果安装IE11,要使PowerShell自动化文档模型,我必须在GAC中(重新)注册mshtml.dll.为什么安装IE11无法确保不会发生这种情况,但我认为IE团队需要对此进行调查.

So to sum up. If you install IE11, to get PowerShell to automate the Document Model, I had to (re-)register the mshtml.dll in the GAC. Why the IE11 installation doesn't ensure this happens is beyond me but I think that the IE team need to look into this.

我还认为,对于那些在IE10/11中可以正常使用"的用户,您必须在已经在GAC中注册了mshtml.dll的计算机上有一个产品(也许是Office,也许是Visual Studio或其他MS应用).因此,为什么您没有看到肯定存在的同一问题.

I also think for those where it 'just works' in IE10/11, you must have a product on the machine that has already registered the mshtml.dll in the GAC (perhaps Office, perhaps Visual Studio or some other MS app). Hence why you are not seeing the same problem that definitely exists.

希望这对某人有帮助-这让我发疯了!

Hope this helps someone - it was driving me crazy!

安德鲁

这篇关于Powershell System .__ ComObject.document属性在IE 9下不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆