2016-09-30 5 views
0

とウェブサイト上の入力ボックスに私は単純にExcelに入力された値を挿入しようとしています:(私は、このVBAコードは動作しません理由を把握しようと、彼は壁に頭を叩いてきたVBA

を埋めます。。ウェブサイトの入力ボックスに入力ボックス、それはので、私はそれはそれとは何かを持っていると確信しているHTMLに来るとき、私は初心者だ

ここでは、ウェブサイトZomato.comからHTML要素です:

<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

は限りに値を入力すると、正しい構文をWebページとして次のようになります。

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 
関連する問題