powershell: how to click a "submit type" input
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