尝试在IE 11中使用Powershell中的getElementById,来自HRESULT的错误异常:0x800A01B6

时间:2015-12-14 17:34:56

标签: powershell internet-explorer-11 powershell-v5.0

我试图在网站上自动登录,但我似乎无法使用getElementById。 getElementsByClassName("无论什么")[0]似乎有用,但我需要的元素没有类名,我无法改变它。

我使用PS 5,IE 11.这是我的代码示例,以及由此产生的异常。

$url = "https://google.com"
$ie = New-Object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate($url)
while ($ie.Busy){Start-Sleep -Milliseconds 1000}
$ie.document.getElementById("lst-ib").value = "test"

错误:

Exception from HRESULT: 0x800A01B6
At C:\gtest.ps1:6 char:1
+ $ie.document.getElementById("lst-ib").value = "test"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], NotSupportedException
    + FullyQualifiedErrorId : System.NotSupportedException

2 个答案:

答案 0 :(得分:2)

登录谷歌

$r = Invoke-WebRequest "https://google.bg" -SessionVariable g

$r = Invoke-WebRequest 'https://accounts.google.bg/' -WebSession $g

$r.Forms['gaia_loginform'].Fields.Email = 'someuser@gmail.com'
$r.Forms['gaia_loginform'].Fields.Passwd = 'somepass'

$r = Invoke-RestMethod $r.Forms['gaia_loginform'].Action -Body $r.Forms['gaia_loginform'] -Method $r.Forms['gaia_loginform'].Method  -WebSession $g

$r = Invoke-WebRequest https://google.bg -WebSession $g

$r.Content > tmp2.html

希望有所帮助

答案 1 :(得分:0)

尝试使用

$ie.Document.documentElement.getElementByClassName();
$ie.Document.documentElement.getElementByTagName();
$ie.Document.documentElement.getAttribute();

,对于ID名称标记名,请使用此

$ie.Document.IHTML3_getElementById();
$ie.Document.IHTML3_getElementByName();
$ie.Document.IHTML3_getElementByTagName();