從this other post看起來可以從Internet Explorer API獲取IHTMLWindow2::execScript
的返回值,但是鏈接的示例是使用C++和I不太瞭解它。我試圖做同樣的事情在PowerShell中,所以這裏是我的代碼,顯示問題:IE COM自動化:如何在PowerShell中獲取`window.execScript`的返回值
$ie = New-Object -COM InternetExplorer.Application
$ie.visible = $true
$ie.navigate("http://google.com")
while ($ie.busy) { start-sleep -m 100 }
$document = $ie.document
$window = $document.parentWindow
Function eval($jsCommand) {
# Of course it doesn't return anything here. How can I get the return value?
return $window.execScript($jsCommand, 'javascript')
}
eval 'parseInt("12")' # returns nothing
我真正要做的是讓jQuery選擇/對象可在我的PowerShell腳本,所以我可以做這樣的事情:
eval '$("input#selector")' | where name -eq 'username'
和更多。
更新:查看此Gist for PowerShell函數以運行JavaScript/JQuery並將結果返回給PS,並且超時。它從下面的答案延伸出來。
[This](http://msdn.microsoft.com/en-us/library/ie/ms536420(v = vs.85).aspx)可能是相關的。顯然,execScript總是返回null。這在IE11中也被棄用 – Matt 2014-09-24 16:40:15
好的發現@Matt現在我試圖計算如何訪問[Global Object](http://msdn.microsoft.com/en-us/library/ie/52f50e9t(v = vs.85).aspx),其中包含[eval](http://msdn.microsoft.com/en-us/library/ie/12k71sw7(v = vs.85).aspx)函數。 – orad 2014-09-24 17:59:16
創建了一個相關的問題[here](http://stackoverflow.com/q/26023915/450913)。 – orad 2014-09-24 21:21:18