试图自动浏览

时间:2017-08-07 18:48:47

标签: powershell internet-explorer ui-automation

我有一个像这样的HTML表单:

<input name="member[email]" id="memberemail" style="width: 220px;" type="text" value="">
<input name="member[password]" id="memberpassword" style="width: 220px;" type="password" value="" autocomplete="off">
<input class="continue" type="submit" value="Log In">

我有一个PowerShell脚本可以验证和浏览页面:这很好用。

$username='xxx@mail.com' 
$password='<password>'




$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate("https://www.ksl.com/public/member/signin?login_forward=%2F")


while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}   

$usernamefield = $ie.Document.getElementByID('memberemail')
$usernamefield.value = $username

$passwordfield = $ie.Document.getElementByID('memberpassword')
$passwordfield.value = $password

$Link=$ie.Document.getElementsByTagName("input") | where-object {$_.className -eq "continue"}



#<INPUT tabIndex=3 id=Logon onclick=this.disabled=true;this.form.submit(); type=submit value="Sign In">
#<input name="ctl00$pageContentPlaceHolder$btnGo" id="ctl00_pageContentPlaceHolder_btnGo" style="width: 200px;" type="submit" value="Sign In">


$Link.click()

然而,当我尝试在ASP.net页面上使用它时,这样:

<input name="ctl00$pageContentPlaceHolder$txtUserID" id="ctl00_pageContentPlaceHolder_txtUserID" style="width: 200px;" type="text" maxlength="30" autocomplete="off">
<input name="ctl00$pageContentPlaceHolder$txtPassword" id="ctl00_pageContentPlaceHolder_txtPassword" style="width: 200px;" type="password" maxlength="256" autocomplete="off">
<input name="ctl00$pageContentPlaceHolder$btnGo" id="ctl00_pageContentPlaceHolder_btnGo" style="width: 200px;" type="submit" value="Sign In">

此修改后的脚本未填写用户名和密码字段

$username='use000' 
$password='<password>'


$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
#ie.Navigate("https://fasttrng.usask.ca/Login.aspx?ReturnUrl=%2f")

while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}   

$usernamefield = $ie.Document.getElementByName('ctl00$pageContentPlaceHolder$txtUserID')
$usernamefield.value = $username

$passwordfield = $ie.Document.getElementByName('ctl00$pageContentPlaceHolder$txtPassword')
$passwordfield.value = $password

$Link=$ie.Document.getElementsByTagName("input") | where-object {$_.id -eq "ctl00_pageContentPlaceHolder_btnGo"}




$Link.click()

我得到了

PS C:\Users\rwm132> T:\work\scripts\Untitled4.ps1

PS C:\Users\rwm132> T:\work\scripts\Untitled4.ps1

PS C:\Users\rwm132> T:\work\scripts\Untitled5.ps1
You cannot call a method on a null-valued expression.
At T:\work\scripts\Untitled5.ps1:11 char:1
+ $usernamefield = $ie.Document.getElementByName('ctl00$pageContentPlac ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At T:\work\scripts\Untitled5.ps1:12 char:1
+ $usernamefield.value = $username
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

You cannot call a method on a null-valued expression.
At T:\work\scripts\Untitled5.ps1:14 char:1
+ $passwordfield = $ie.Document.getElementByName('ctl00$pageContentPlac ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At T:\work\scripts\Untitled5.ps1:15 char:1
+ $passwordfield.value = $password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

You cannot call a method on a null-valued expression.
At T:\work\scripts\Untitled5.ps1:17 char:1
+ $Link=$ie.Document.getElementsByTagName("input") | where-object {$_.i ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At T:\work\scripts\Untitled5.ps1:22 char:1
+ $Link.click()
+ ~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException


PS C:\Users\rwm132> 

ASP.Net渲染输入字段有什么特别之处吗?

1 个答案:

答案 0 :(得分:0)

该脚本适用于静态html而不是ASP.Net有人建议它无法处理正在呈现的AJAX部分,这听起来不错吗?我认为在.NET中这样的网络形式是e