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

Categories

I'm getting this error in a simple PowerShell script. "getElementByID" always takes 1 argument, so I'm not sure why this is failing. I'm using IE 11 and PowerShell 3 running on WS2K8 r2.

$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$true
$ie.navigate("http://duckduckgo.com") 
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("search_form_input_homepage").value = "foobar"
$ie.document.getElementById("search_button_homepage").Click()

resulting error message:

Cannot find an overload for "getElementById" and the argument count: "1". At C:SCRIPTSsample.ps1:5 char:1 + $ie.document.getElementById("search_form_input_homepage").value = "foobar" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest

See Question&Answers more detail:os

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

1 Answer

Thanks SiZiIOUS for the link - eventually that led me to try testing for null references - it seems that during the redirect from HTTP to HTTPS at least some part of the COM object is "lost".

After resetting things back to defaults (including Internet Zone security settings), it appears that the two things I need to do to get this to work without running PowerShell as administrator are:

  1. Disable IE Enhanced Security Configuration
  2. Add the desired URL to Compatibility View

Also worth noting - if after this I add the URL to Trusted Sites, it actually stops working again, which I suspect caused me to miss the solution at some point previously since when I tried this combination of settings it was probably listed in Trusted Sites. Why that would be, I'm not sure, but maybe it will spark a thought for someone who can explain.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...