0
許多帖子沿着我所知道的相同的路線,但是我們真的很努力使這個工作。VBA IE Form Automation
我創建的代碼登錄到一個網站,點擊一個按鈕,打開一個帶有窗體的新選項卡,但是我無法在表單中輸入數據,儘管嘗試了其他帖子的各種示例。
我此刻的代碼是:
''=======================================================
'' Program: MyQuote
'' Desc: Writes Data into online web form from data in workbook
'' Called by: Batch File (MyQuote.bat)
'' Call: MyQuote
'' Info:
''This macro has been built improve the time it takes to gain a quotation from the MyQuote website.
''Once an agent has completed a quote on the inhouse system, they will run batch file (MyQuote.bat) which FTP's out the quote data, imports it into this
''workbook, and uses that data to fill the quote forms on the MyQuote website.
''The macro creates a new instance of Internet Explorer, navigates to the site, enters the username and password to login, and navigates through
''each question entering the variable extracted from our quote system.
'' Changes----------------------------------------------
'' Date Programmer Change
'' 13/01/2017 Craig Written
''=======================================================
Sub MyQuote()
'Declaring the necessary variables.
Dim IE As Object
Dim Bton1 As HTMLGenericElement
'Set browser
Set IE = CreateObject("InternetExplorer.Application")
'Log Off if already logged in
IE.navigate "http://www.example.com/logout"
'Wait until IE has loaded
Do Until IE.readyState = 4
DoEvents
Loop
'Navigate to My Quote Website
IE.navigate "http://www.example.com/"
'Wait until IE has loaded
Do Until IE.readyState = 4
DoEvents
Loop
'Enter values and login to My Quote Website
IE.document.all.Item("UserName").Value = "[email protected]"
IE.document.all.Item("Password").Value = "abc"
'Find and press the submit button
Set objCollection = IE.document.getElementsByTagName("button")
If objCollection(i).Type = "submit" Then
Set objElement = objCollection(i)
End If
objElement.Click
'Wait while IE re-loads...
While IE.Busy
DoEvents
Wend
'Find and press the New Quote button
Set objCollection = IE.document.getElementsByTagName("button")
If objCollection(i).className = "btn btn-success btn-xs" Then
Set objElement = objCollection(i)
End If
objElement.Click
'Wait while IE re-loads...
While IE.Busy
DoEvents
Wend
'Find and press the Agree button on Credit Check pop up overlay
For Each l In IE.document.getElementsByTagName("button")
If l.className = "btn btn-success" Then l.Click
Next
'Wait while IE re-loads...
While IE.Busy
DoEvents
Wend
'Enter Registration
'=====below is what is not working======
'IE is now in a new tab with different URL if that helps
IE.document.all.Item("Registration").Value = "AB12CDE"
End With
End Sub
我無法提供實際的URL /密碼等網站,但是下面是網頁和元素的HTML片段我試圖讓工作:
<input type="text" name="Registration" id="Registration" class="form-control reg-input" data-bind="value: Registration" placeholder="REGISTRATION">
<button id="btn-findVehicle" class="btn btn-primary" type="button" data-bind="click: $parent.findVehicle">Find your car </button>
我不知道這是因爲我想選擇一個對象,我現在是在一個新的IE選項卡,但我花了10小時,試圖讓這工作沒有任何運氣。
一旦解決了這個問題,我可以繼續繪製其他40個字段,單選按鈕,下拉菜單的&提交按鈕(祝我好運)。
任何幫助和建議表示讚賞。
問候
克雷格
感謝您的回答阿倫,我也習慣了ID元素。這實際上是由於瀏覽器導航到導致問題的另一個選項卡。我只是導航到它在當前選項卡中打開的頁面,並且一切正常。 謝謝 –