2016-09-30 40 views
0

我一直在敲打我的頭靠在牆上,他試圖找出爲什麼這個VBA代碼將無法正常工作:(填寫輸入框的網站使用VBA

我只是試圖插入輸入到Excel中的價值。輸入框到網站的輸入框,我是新手,當涉及到HTML,所以我敢肯定,有什麼關係呢

下面是從網站的HTML元素Zomato.com:

<input class="dark" id="location_input" role="combobox" aria-expanded="true" aria-labelledby="label_search_location" aria-owns="explore-location-suggest" aria-autocomplete="list" placeholder="Please type a location..."> 

這是我的VBA代碼:

Sub Merchant_Extraction() 

Dim IE As Object 
Dim form As Variant 
Dim button As Variant 

Set IE = CreateObject("internetexplorer.application") 

merchantzip = InputBox("Enter Zip Code") 

With IE 

.Visible = True 
.navigate ("http://www.zomato.com") 

While IE.readystate <> 4 
DoEvents 
Wend 

IE.Document.GetElementByID(「location_input_sp」).Item.innertext = merchantzip 

Set form = IE.Document.getelementsbytagname("form") 

Set button = form(0).onsubmit 
form(0).get 

End With 

Set IE = Nothing 

End Sub 

我不清楚它爲什麼不能正常工作 - 任何幫助都是不可思議的!

+0

爲什麼你在刮? [只需使用他們的API](https://developers.zomato.com/documentation)。 – Comintern

回答

0

至於將值輸入網頁的正確語法是:

IE.Document.all.Item("location_input").Value = "" 

我結合一些代碼,我用這樣你就可以看到一個例子你的例行公事。然而,我還沒有能夠測試。在我的環境中,IE對象在.navigate部分斷開連接後斷開,因此我添加了一個循環以查找並重新分配對象...

Option Explicit 
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 


Sub Merchant_Extraction() 
Dim IE As Object, objShellWindows As Object 
Dim MerchantZip As String, strWebPath As String 
Dim Form As Variant, Button As Variant 
Dim X As Long 

strWebPath = "http://www.zomato.com" 

MerchantZip = InputBox("Enter Zip Code") 
If MerchantZip = vbNullString Then Exit Sub 

Set IE = CreateObject("InternetExplorer.Application") 
With IE 
    .Visible = True 
    .Navigate strWebPath 
End With 

Do 
    Sleep 250 
    DoEvents 
Loop While IE.Busy Or IE.ReadyState <> 4 

If TypeName(IE) <> "IWebBrowser2" Or IE.Name <> "Internet Explorer" Then 
    Set objShellWindows = CreateObject("Shell.Application").Windows 
    For X = 0 To objShellWindows.Count - 1 
     Set IE = objShellWindows.Item(X) 
     If Not IE Is Nothing Then 
      If IE.Name = "Internet Explorer" Then 
       If InStr(1, IE.LocationURL, strWebPath, 1) > 0 Then 
        Do While IE.Busy Or IE.ReadyState <> 4 
         Sleep 250 
         DoEvents 
        Loop 
        Exit For 
       End If 
      End If 
     End If 
     Set IE = Nothing 
    Next 
    Set objShellWindows = Nothing 
End If 

If Not IE Is Nothing Then 
    IE.Document.all.Item("location_input").Value = MerchantZip 
    Sleep 250 
    For Each Button In IE.Document.getelementsbytagname("form") 
     If StrComp(Button.Type, "Button", 1) = 0 Then 
      Button.Click 
     End If 
    Next 
    Set IE = Nothing 
End If 

End Sub