2017-06-22 48 views
0

我在嘗試使用Excel的IE自動化工具單擊本網站用戶配置文件中的網站鏈接。 VBA中使用以下:單擊HTML列表中的鏈接

ie.Document.getElementsByClassName("website")(0).getElementsByTagName("a")(0).Click 

但不斷收到運行時錯誤「438」跑時。有關如何解決這個問題的任何建議,或者點擊這個鏈接,甚至可能嗎?謝謝。

EDIT(全碼):

Dim ie As InternetExplorer 
Dim html As HTMLDocument 
Set ie = New InternetExplorer 
ie.Visible = True 
ie.Navigate "site" 

Do While ie.READYSTATE <> READYSTATE_COMPLETE 
DoEvents 
Loop 

Set objA = ie.Document.getElementsByClassName("website")(0) 'GETTIN ERROR HERE 
objA.getElementsByTagName("a")(0).Click 
End Sub 
+0

你能在鏈你得到'#錯誤識別438'在什麼時候?這可能是您如何使用Document對象的問題。 –

+0

我已經添加了上面的完整代碼並將其稍微分開以查看發生錯誤的位置。似乎是在搜索類名時。 –

回答

0

這樣看來,有幾個小問題在這裏。

其中之一是.Click總是不正常運行,我會建議使用.getAttribute("href")ie.Navigate()

,你似乎定義HTML則不會再使用它彼此不結合。

下面的代碼工作:

Sub test() 
Dim ie As InternetExplorer 
Dim html As HTMLDocument 
Set ie = New InternetExplorer 
ie.Visible = True 
ie.Navigate "http://beverlyhills.yourkwoffice.com/mcj/user/AssociateSearchSubmitAction.do?orgId=5058&lastName=&firstName=&rows=100" 

Do While ie.READYSTATE <> READYSTATE_COMPLETE 
DoEvents 
Loop 

Set html = ie.Document 

Set objA = html.getElementsByClassName("website")(0).getElementsByTagName("a")(0) 
ie.Navigate (objA.getAttribute("href")) 
End Sub 
+0

非常感謝!很有幫助!!! –