powershell: how to click a “submit type” input

℡╲_俬逩灬. 提交于 2019-12-01 09:22:07

You're looking for the wrong element. getElementsByTagName() is looking for the actual tag name (input), not the value of the tag's name-attribute (commit). Also, getElementsByTagName() returns a collection of COM objects. Even if no matching tag is found, the method will still return a collection (with 0 elements). You need to either check the Length property and then access the first element of the collection:

$commit = $doc.getElementsByTagName("input")
if ($commit.Length -gt 0) {
  $commit.item(0).click()
}

or filter the element with the name you're looking for from the collection:

$commit = $doc.getElementsByTagName("input") | ? { $_.name -eq "commit" }
if ($commit) { $commit.click() }

I could not access the url you had listed above so I used the MIT website to show you an example of how can this be done.

# setup
$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$true

$ie.navigate("http://web.mit.edu/") 
while($ie.ReadyState -ne 4) {start-sleep -m 100} 

$termsField = $ie.document.getElementsByName("terms")
@($termsField)[0].value ="powershell"


$submitButton = $ie.document.getElementsByTagName("input") 
Foreach($element in $submitButton )
{
    #look for this field by value this is the field(look for screenshot below) 
    if($element.value -eq "Search"){
    Write-Host $element.click()
    }
}

    Start-Sleep 10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!