2011-02-14 32 views
1

在這裏,我們去...憋屈實現WebBrowser控件和工作與AJAX響應

我想創建一個機器人通過一個網站,我沒有控制的各種功能走路。最初,我認爲最好是連接到數據庫(MySql),該網站是綁定的,並在那裏建立我的關聯......數據庫的構建如此廣泛,以至於我無法確定將指向何處的地方等等......它超出了我的(DBA)程序員的範圍......;) 所以,我的下一個想法是,創建一個機器人......簡單得夠嗎?第一個障礙,並非頁面上的所有內容都帶有ID ...帶上循環...

明白了。

現在我堅持使用我的數據和頁面響應。

我想填寫表格的一部分並執行AJAX搜索。問題是,這沒有DocumentCompleted。說實話,這不是我的問題所在。我試過使用Thread.Sleep,定時器等...無濟於事。

// The app reads categories from a csv file, 
// then performs a search for the category 
// Search results are only displayed if I break the foreach loop 
foreach (var item in bar) 
{ 
var document = wbBrowser.Document; 
if (document != null) 
{ 
    var name = document.GetElementById("product_filter_name"); 
    if (name != null) 
    { 
     name.SetAttribute("value", item.Key.ToString()); 

     var buttons = document.GetElementsByTagName("button"); 
     foreach (HtmlElement button in buttons) 
     { 
      var findSearch = button.InnerHtml.IndexOf("Search"); 

      if (findSearch > -1) 
      { 
       button.InvokeMember("click"); 
      } 
     } 
    } 

    // This where the problem starts... 
    // I want the AJAX to run, then perform Step two, 
    // but the WebBrowser doesn't return the search 
    // results until the end (break;) 
    // Step Two 

    var elems = document.GetElementsByTagName("tr"); 
    foreach (HtmlElement elem in elems) 
    { 
     // find particular item in result table 
    } 

    break; 

    // Now the search results display!!!! 
    // I tried implementing a timer, Thread.Sleep, 
    // everything I could find via Google before 
    //starting Step Two, but it hasn't worked! 
} 

}

回答

0

實際的瀏覽器控件有一個WebBrowser.OnDocumentCompleted事件,您可能需要掛接到,這樣就可以當Ajax調用返回從服務器返回的提醒。

+0

這並不總是在ajax請求上正確觸發,具體取決於所處的幀。當結果元素尚未渲染時,它也可以觸發多次。 – Jakub 2011-02-14 20:38:53