如何使Powershell与网页上的元素进行交互

时间:2018-10-12 15:00:26

标签: html powershell powershell-v6.0 htmldocumentclass

我正在尝试编写一个Powershell脚本,该脚本将与网页的元素进行交互。为了学习脚本的概念,我使用Armorgames.com作为目标URL,旨在使用powershell登录该站点,然后根据指定的搜索词搜索可用的游戏,然后将结果导出到CSV 。所附的代码示例是我目前拥有的。但是我无法理解我要搜索的网页元素的哪些部分以及如何将值输入到可用字段中。 我还收到错误消息“方法调用失败,因为[mshtml.HTMLDocumentClass]不包含名为'getElementsById'的方法”,并且我不理解为什么

$ie = New-Object -ComObject "InternetExplorer.Application"
$requestUrl = "https://armorgames.com/"
$userId = "username-input";
$passwordId = "password-input"
$signIn = "act"

$ie.visible = $true
#ie.silent = $true
$ie.navigate($requestUrl)
while ($ie.busy) {
    Start-Sleep -Milliseconds 100
}
$doc = $ie.Document
$doc.getElementsById("input") | % {
    if ($_.id -ne $null){
        if ($_.id.Contains($signIn)) { $btn = $_ }
        if ($_.id.Contains($passwordId)) { $pwd = $_ }
        if ($_.id.Contains($userId)) { $user = $_ }
    }
}

$user.value = "ExampleUser"
$pwd.value = "MyPassord"

$btn.disabled = $false
$btn.click()
Write-Output $ie
Write-Output "doc is $doc"

0 个答案:

没有答案