Powershell - 单击HTML代码上的提交按钮

时间:2014-03-11 11:05:36

标签: powershell

我尝试了很多不同的方法来点击HTML代码上的提交按钮,但总是出错。

<form action="abnormal-times.aspx" method="post">
Time out Minutes <input name="Minutes" style="width: 100px;" value="5"> Minutes.<br>
Time out Hours <input name="Minutes" style="width: 100px;" value="2"> Hours.
<input onclick="submit()" type="button" value="submit">

使用我的powershell代码:

$ie = New-Object -com InternetExplorer.Application 
$ie.visible = $true
$ie.navigate("http://localhost.com")

while($ie.ReadyState -ne 4) { start-sleep -s 1 }

$Link = $ie.Document.getElementsByValue("submit")
$Link.click()

但总是弹出错误。

Method invocation failed because [mshtml.HTMLDocumentClass] doesn't contain a method
 named 'getElementsByValue'.
At C:\temp\Untitled1.ps1:28 char:42
+ $Link = $ie.Document.getElementsByValue <<<< ("submit")
    + CategoryInfo          : InvalidOperation: (getElementsByValue:String) [], Run 
   timeException
    + FullyQualifiedErrorId : MethodNotFound

You cannot call a method on a null-valued expression.
At C:\temp\Untitled1.ps1:29 char:14
+ $Link.click <<<< ()
    + CategoryInfo          : InvalidOperation: (click:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

现在我换成了这个:

$Link=$ie.Document.getElementsByTagName("button") | where-object {$_.type -eq "submit"}
$Link.click()

错误出现了:

You cannot call a method on a null-valued expression.
At C:\Users\Work_Server\Desktop\joseph.ps1:29 char:10
+ $go.click <<<< ()
    + CategoryInfo          : InvalidOperation: (click:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

1 个答案:

答案 0 :(得分:5)

按钮的标记名称input类型button。试试这个。

$Link=$ie.Document.getElementsByTagName("input") | where-object {$_.type -eq "button"}
$Link.click();