可以循環通所有輸入標籤,並通過檢查「最具識別性」屬性(id,name,type,innerHTML,... whatever)來識別相關標籤。這裏的一個小組()我在Excel工作表可以自動登錄到網站使用
Sub FormAction(Doc As MSHTML.HTMLDocument, ByVal Tag As String, ByVal Attrib As String, ByVal Match As String, ByVal Action As String)
Dim ECol As MSHTML.IHTMLElementCollection
Dim IFld As MSHTML.IHTMLElement
Dim Tmp As String
Set ECol = Doc.getElementsByTagName(Tag)
For Each IFld In ECol ' cycle thru all <[tag]> elements
If VarType(IFld.getAttribute(Attrib)) <> vbNull Then ' does it contain the attribute
If Left(IFld.getAttribute(Attrib), Len(Match)) = Match Then
If Action = "/C/" Then
IFld.Click
Else
IFld.setAttribute "value", Action
End If
Exit Sub
End If
End If
Next
End Sub
該函數採用以下參數:
- 文件....的HTML DOM對象
- 標籤....你想要處理的標籤(通常是輸入,圖像,a,...)
- 屬性....你想要匹配的值的屬性
- 匹配....匹配值爲屬性
- 行動.....如果在匹配標籤進行「/ C /」的。點擊()動作,其他動作值放入變量的值屬性
如果你想勾上包含的JavaScript代碼(如「點擊」)不要忘記,你的HTML源看到代碼在一個匿名函數內嵌任何屬性,就像
function anonymous()
{
onclick="exportData(workOrderSearchForm)";
}
您需要使用的可以考慮一下Instr()函數或類似的,如果你想掛鉤例如「exportData(workOrder)」。
也非常重要:給頁面足夠的時間導航到DOM對象並加載。我注意到我的公司,一旦頁面被更改,加載時間有時會大幅增加。
我有這個良好的成功:
Sub MyMainSub()
Dim Browser As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
' my other Dim's
' start browser
Set Browser = New SHDocVw.InternetExplorer
Browser.navigate "http://www.whatever.com"
WaitForBrowser Browser, 5 ' wait for browser, but not more than 5 sec
' gain control over DOM object
Set HTMLDoc = Browser.document
WaitForBrowser Browser, 5 ' wait for browser, but not more than 5 sec
' do actions
' FormAction HTMLDoc, w, x, y, z
End Sub
Sub WaitForBrowser(Browser As SHDocVw.InternetExplorer, Optional TimeOut As Single = 10)
Dim MyTime As Single
MyTime = Timer ' seconds since midnight
Do While Browser.Busy Or (Timer <= MyTime + TimeOut)
DoEvents ' go do something else
Loop
If Browser.Busy Then
MsgBox "I waited for " & Timer - MyTime & " seconds, but browser still busy" & vbCrLf & _
"exititing Login sequence now"
End
End If
End Sub
希望這是鼓舞人心的足夠爲您打造您的解決方案。
祝你好運MikeD
不幸的是,它說這不是一個支持的方法或屬性。 「if.window.exportData(ie.Document.forms.workOrderSearchForm)」一旦放入,它也將代碼間隔爲空。 – Michael 2010-07-14 15:24:29
嘗試'ie.window.exportData ie.window.workOrderSearchForm'或'ie.window.eval 「exportData(workOrderSearchForm)」' – SLaks 2010-07-14 15:25:27
這兩者似乎都無法做到這一點。他們產生相同的錯誤。 – Michael 2010-07-14 15:31:59