Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

internet explorer 9 - Powershell System.__ComObject.document property no longer works under IE 9

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

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

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

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}

But under IE 8 I get:

  TypeName: mshtml.HTMLDocumentClass

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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. Using the dev console in IE11, switch the Document Mode to 8 (9,10,11,Edge (default) don't work) - my automation script works instantly. No changes to IE trusted sites, zone security, protected mode, PowerShell session priveleges). So clearly just a component issue of the IE11 installation of some sort
  2. Installing Office 2013, without ever running or licensing it, the same script works instantly without changing the Document Mode of IE11. Clearly Office does install/register something that fixes the problem (as Rhys Edwards says)

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

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

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

  3. Took a VM snapshot

  4. Used Regshot to do record the registry and file system

  5. 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

  6. Took a 2nd VM snapshot

  7. Ran 2nd scan with regshot and analysed the differences

  8. 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

  9. 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. I located the microsoft.mshtml.dll in the "c:program files(x86)Microsoft.netprimary interop assemblies" folder and saved it out of the VM to my local machine desktop

  2. reverted to the pre-office 2013 snapshot

  3. 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:windowsassembly view in explorer. In later versions of .Net you need to use gacutil /l

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

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.

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!

Andrew


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...