2014-10-10 46 views
0

我正在使用VBA填充URL中的表單並提交以獲取結果。 當我使用正確的值提交表單並通過VBA提交時,我在另一個URL中獲得結果,但在同一窗口中。VBA在另一個網址但在同一窗口中獲取結果

問題是,我不知道如何改變html引用來開始使用這個新的url來報廢數據。

這裏是我的代碼:

'to refer to the running copy of Internet Explorer 
    Dim IE As InternetExplorer 
    'to refer to the HTML document returned 
    Dim html As HTMLDocument 
    'open Internet Explorer in memory, and go to website 
    Set IE = New InternetExplorer 
    IE.Visible = False 
    IE.navigate "http://url..." 
    'Wait until IE is done loading page 
    Do While IE.READYSTATE <> READYSTATE_COMPLETE 
     Application.StatusBar = "Connecting with http://url..." 
     DoEvents 
    Loop 
    'show text of HTML document returned 
    Set html = IE.document 
    'close down IE and reset status bar 
    Set IE = Nothing 
    Application.StatusBar = "" 

    Set txtArea = html.getElementsByTagName("textarea")(0) 
    txtArea.Value = txtArea_data 

    Set formSubmit = html.getElementsByName("submit")(1) 
    formSubmit.Click 

    '-------------Get results 
'Dim html_results As HTMLDocument 
IE.navigate "http://new_url" 'Im not sure if I must do it this way... 
'Wait until IE is done loading page 
Do While IE.READYSTATE <> READYSTATE_COMPLETE 
    Application.StatusBar = "Connecting with http://new_url..." 
    DoEvents 
Loop 
Set html = IE.document 
Dim trResults As IHTMLElementCollection 
Set trResults = html.getElementsByClassName("tr") 
MsgBox (trResults.Length) 'At this point, trResults always have 0 results... 

你有什麼想法幫助我嗎?

謝謝!

+1

至少,當您設置IE = Nothing時,您無法在後續命令中繼續使用InternetExplorer對象。此外,提交表單應該帶您到另一個網頁,但您不要等待它加載,然後再嘗試''.Navigate'到一個新的URL。我強烈懷疑表單提交是不正確的,但這是猜測而沒有看到頁面的來源。 – Jeeped 2014-10-10 12:01:18

回答

0

這是一種新方法,但仍然不起作用。 我已使用IE.Visible = True,所以我可以檢查結果頁面上是否有多個<tr>結果。

Dim rowNumber As Long 
Dim txtArea_data As String 
Dim txtArea As Object 
Dim formSubmit As Object 

Dim IE As InternetExplorer 

Dim html As HTMLDocument 

Set IE = New InternetExplorer 
IE.Visible = True 

IE.navigate "http://url..." 

Do While IE.READYSTATE <> READYSTATE_COMPLETE 
    Application.StatusBar = "Connecting with http://url..." 
    DoEvents 
Loop 

Set html = IE.document 

rowNumber = 4 
For rowNumber = 4 To 120 'Rows.Count 

    txtArea_data = txtArea_data & Cells(rowNumber, 1).Value & Chr(10) 

Next rowNumber 

Set txtArea = html.getElementsByTagName("textarea")(0) 
txtArea.Value = txtArea_data 

Set formSubmit = html.getElementsByName("submit")(1) 
formSubmit.Click 

'Wait until results 
Do While IE.Busy: DoEvents: Loop 

'-------------Get results 
'At this point, the page URL with results has changed, but on the same tab. In the other code, I've used IE.navigate "http://new_url..." But a "Invalid file" message appears. 
'------------- 
'I suppose html var could be recharged with these new results data, but nothing happen... 

Set html = IE.document 
Dim trResults As IHTMLElementCollection 
Set trResults = html.getElementsByClassName("tr") 

MsgBox (trResults.Length) 'At this point, MsgBox returns O... 

Set html = Nothing 

結果頁面的樣子:

<body> 
    <table id="tabla-a"> 
    <thead> 
     <tr> 
     <th>...</th> 
     <th>...</th> 
     <th>...</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
     <td>...</td> 
     <td>...</td> 
     <td>...</td> 
     </tr> 
     <tr> 
     <td>...</td> 
     <td>...</td> 
     <td>...</td> 
     </tr> 
     <tr> 
     <td>...</td> 
     <td>...</td> 
     <td>...</td> 
     </tr> 
    </tbody> 
    </table> 
</body> 

有什麼我可以告訴你,使幫你找到問題? 謝謝!

編輯

哦,我的上帝!我一直在使用Set trResults = html.getElementsByClassName("tr")而不是Set trResults = html.getElementsByTagName("tr") !!!

謝謝你的時間!

現在該怎麼辦?用最終解決方案編輯原始問題或關閉整個問題? 我不會使用Stackoverflow太多問問題...

謝謝!

相關問題