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