2011-05-10 43 views
0

我嘗試使用PowerShell來登錄到以下HTML形式的問題,登錄到該格式混亂的HTML表單:HTML更新以顯示登錄按鈕如何使用PowerShell

<tr> 
<tr><td align='right' valign=top bgcolor='#ffffff' width=15%><font face='Arial, Helvetica, Verdana' size=-1 color=#000000 id=copy>User name or Email:</font></td><td valign=bottom><input type='text' name=AutoLogin size=30><tr><td align='right' valign=top bgcolor='#ffffff' width=15%><font face='Arial, Helvetica, Verdana' size=-1 color=#000000 id=copy>Password:</font></td><td valign=bottom><input type='password' name=AutoPassword size=30> 

</tr></table> 
<br> 
<table cellpadding=4 cellspacing=1 border=0 bordercolor=#ffffff width=100% height=*> 

<tr> 
<tr><td><input type=submit value="Log In"> 

PowerShell的代碼登錄頁面:

& "$env:programfiles\Internet Explorer\iexplore.exe" 'https://login-url.asp' 

$win = New-Object -comObject Shell.Application 
$try = 0 
$ie2 = $null 
do { 
Start-Sleep -milliseconds 500 
$ie2 = @($win.windows() | ? { $_.locationName -like '*Log In*' })[0] 
$try ++ 
if ($try -gt 20) { 
Throw "Web Page cannot be opened." 
} 
} while ($ie2 -eq $null) 

$ie2.document 
$ie2.Document.body.innerHTML 

$doc = $ie2.document 

$username = 'xxxxx' 
$password = 'xxxxx' 

#username field 
$usernameField = $doc.getElementsByName("desc") 
Write-Host "Username field is?" 
$usernameField 

#password field 
$passwordField = $doc.getElementsByName("password") 
Write-Host "Password field is?" 
$passwordField 

$submitButton = $doc.getElementById('submit') 
$submitButton.click() 

do {sleep 1} until (-not ($ie2.Busy)) 
+0

嗯,恕我直言,你只需要名稱爲'desc'和'密碼' – 2011-05-10 08:51:22

+0

的元素('getElementsByName'),它會拋出以下錯誤: '您無法在空值表達式上調用方法。 在E:\ getNotes.ps1:14字符:48 + $ usernameField = $ doc.getElementsByName <<<<( 「降序」) + CategoryInfo:InvalidOperation:(getElementsByName:字符串)[],的RuntimeException + FullyQualifiedErrorId: InvokeMethodOnNull' 然後我得到以下輸出: '用戶名字段是?: 的className: onhelp: 的onclick: onfocus此:' - 並在不斷... – 2011-05-10 09:07:09

+0

什麼是'$ doc'?你用'new-object -com InternetExplorer.Application'和'.... Navigate2(「http:// yoururl」)''來創建它嗎?你能不能展示更多的代碼? – 2011-05-10 09:20:33

回答

0

爲了使用的輸入表單名稱按下按鈕,所有得到它的工作所需要的代碼是:

#username field 
$usernameField = $doc.getElementsByName("desc") 
($usernameField |select -first 1).value = "xxxxx" 

#password field 
$passwordField = $doc.getElementsByName("password") 
($passwordField |select -first 1).value = "xxxxx" 

$ie2.document.getElementById("main").submit()