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

Trying to use getElementById in Powershell with IE 11, error Exception from HRESULT: 0x800A01B6

我正在尝试自动登录网站,但我似乎无法使用 getElementById。 getElementsByClassName("whatever")[0] 似乎有效,但我需要的元素没有 class 名称,我无法更改它。

我正在使用 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

正在登录 google

$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

希望对您有所帮助

尝试使用

$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();