3

我正在通過Powershell訪問InternetExplorer.Application COM。我試圖使用documentquerySelector,但它不返回任何結果。我目前正在使用IE8。querySelector所有的IE COM在IE 8中都不起作用

$ie = New-Object -com "InternetExplorer.Application" 
$ie.visible = $true 
$ie.Navigate("www.google.com") 


if ($ie.Busy) { Start-Sleep 1 } 

# This statement works 
@($ie.Document.getElementsByTagName("a"))[0] 

# This statement doesn't work, though querySelectorAll 
# exist in the document object 
@($ie.Document.querySelectorAll("a"))[0] 

# I tried this 
$ie.Document.querySelectorAll("a") -eq $null # evaluates to True 
+0

你會得到什麼錯誤信息?它返回什麼? – ManseUK

+0

'querySelectorAll'不返回任何東西。它返回null。 – OnesimusUnbound

+0

在我的機器上嘗試了您的代碼,並得到:「方法調用失敗,因爲[mshtml.HTMLDocumentClass]不包含名爲'querySelectorAll'的方法。」 –

回答

0

使用IDocumentSelector前綴引用方法,使用這樣的事情:

$ie.Document.IDocumentSelector_querySelectorAll("a") 

onetwo PowerShell的瀏覽器API故障排除資源,這可能會有幫助。 Sizzle可能會有所幫助,如果doctype導致瀏覽器渲染幾乎是標準或怪癖模式,它們不支持querySelectorAll。

+0

'$ ie.Document.IDocumentSelector_querySelectorAll(「a」)'沒有奏效。無論如何,我還沒有探索你發佈的鏈接。我會在週末試試看。 – OnesimusUnbound

+0

我需要一個CSS選擇器功能來搜索簡單的ID或名稱元素名稱。嗯,Sizzle可能會訣竅。無論如何,我會在本週內嘗試。 – OnesimusUnbound