我有一個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='[email protected]'
$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">
這modiified腳本不填寫用戶名和密碼字段
$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>
Is there ASP.Net呈現的輸入字段有什麼特別之處?
只是一個猜測,但你需要逃避'$'標誌嗎?或者你甚至爲什麼從'getElementByID'改爲'getElementByName'? –
是的,我最初有BYID,但這也沒有工作......這是一個.NET頁面有人建議這種方法將無法與AJAX工作。不知道是否因爲你看到名稱=「ctl00 $ pageContentPlaceHolder $ txtUserID」id =「ctl00_pageContentPlaceHolder_txtUserID」表明它是一個AJAX頁面我雖然這是純粹的服務器端ASP.NET?你怎麼看JamesZ –
我還在猜測逃跑$,不知道是不是這個問題,但不知道還有什麼可能會這麼嚴重錯誤 –