4

I'm trying to automate logging in on a website, but I can't seem to use getElementById. getElementsByClassName("whatever")[0] seems to work, but the elements I need don't have class names and I can't change that.

I'm using PS 5, IE 11. Here's an example of my code, and the resulting exception.

$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"

Error:

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

2 回答 2

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

希望有帮助

于 2015-12-14T20:16:06.217 回答
1

尝试使用

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

对于 id name tagname 使用这个

$ie.Document.IHTML3_getElementById();
$ie.Document.IHTML3_getElementByName();
$ie.Document.IHTML3_getElementByTagName();
于 2018-08-10T14:40:36.423 回答