2011-03-02 54 views
5

我正在研究一個簡單的應用程序,該應用程序會在包含兩個下拉菜單和一個按鈕的頁面中自動瀏覽。該網頁看起來是這樣的:c#webBrowser.Document:在回發後重新加載頁面

------ DropDown1 -------

------ DropDown2 -------

- -----按鈕---------

現在,問題是,通過Dropdown1選擇是動態生成的DropDown2內容。

我在C#寫了這樣的代碼:

private void webBrowser1_DocumentCompleted(object sender, 
     WebBrowserDocumentCompletedEventArgs e) 
{ 
    HtmlElement elem = webBrowser1.Document.GetElementById("DropDown1"); 
    elem.SetAttribute("selectedIndex", "1"); 
    elem.RaiseEvent("onChange"); 
    HtmlElement elem = webBrowser1.Document.GetElementById("DropDown2"); 
    elem.SetAttribute("selectedIndex", "5"); 
    elem.RaiseEvent("onChange"); 
} 

提高onChange事件之後,瀏覽器加載新的值,但我不能獲取和設置DropDown2值,因爲文件仍然認爲DropDown2價值觀是空的。

如何獲取並設置在DropDown2中生成的新值?

回答

2

我發現onChange事件後通過調用「__doPostBack」腳本找到解決方案。當我調用doPostBack時,文檔重新加載,所以我可以檢索新的值。繼承人的代碼:

private void BeginOperation() 
    { 
     webBrowser1.Navigate("somewebpage", false); 
     Task = 0; 
    } 
    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     HtmlElement elem; 

     switch (Task) 
     { 
      case 0: 
       //HtmlDocument mydoc = webBrowser1.Document; 
       elem = webBrowser1.Document.GetElementById("ddlCity"); 
       MessageBox.Show(elem.All.Count.ToString()); 
       elem.SetAttribute("selectedIndex", "1"); 
       //elem.RaiseEvent("onChange"); 
       object[] args = {"someparameters"}; 
       webBrowser1.Document.InvokeScript("__doPostBack",args); 
       Task++; 
      break; 
      case 1: 
       elem = webBrowser1.Document.GetElementById("ddlDistrict"); 
       elem.SetAttribute("selectedIndex", "2"); 
       elem.RaiseEvent("onChange"); 
       object[] args2 = {"someparameters"}; 
       webBrowser1.Document.InvokeScript("__doPostBack",args2); 
       Task++; 
      break; 
     } 
    } 
0

我懷疑你遇到的問題是因爲你寫的代碼不等待回發發生。所以會發生什麼是....

 
|---> The page finishes loading, triggering your DocumentCompleted method 
|---> You set the selectedIndex on DropDown1 
|---> You raise the onChange event for DropDown1 
|  |---> The page starts posting-back (1) 
|---> You (attempt to) set the selectedIndex on DropDown2 
|---> You raise the onChange event for DropDown2 
|  |---> The page starts posting-back (2) 
| 
... 
... 
... 
|---> The page finishes re-loading from from postback (2) 

基本上,你需要做的觸發回發的頁面重新加載後等待什麼。不雅,脆弱和幾乎肯定要打破/不工作的方式是觸發Timer或類似的操作,以便經過一段時間(只要回發發生),然後可以繼續設置selectedIndex爲DropDown2。更好的選擇是做這樣的事情:

 
|---> The page finishes loading, triggering your DocumentCompleted method 
|---> You attach a new EventHandler to DocumentCompleted that contains the 
|  code for changing the selectedIndex on DropDown2 and REMOVE this 
|  eventhandler 
|---> You set the selectedIndex on DropDown1 
|---> You raise the onChange event for DropDown1 
|---> Your code in the DocumentCompleted handler finishes executing 


|---> // This is the DocumentCompleted handler that you assign above 
|---> You set the selectedIndex on DropDown2 
|---> You raise the onChange event for DropDown2 
|---> Your code in the DocumentCompleted handler finishes executing 

有這樣做的更優雅的方式,但是這可能是最簡單的解釋。

+0

我想到了這一點,但「DocumentCompleted」處理程序沒有運行後回發。在dropDown1中選擇一個項目只會觸發一個函數來填充DropDown2。所以Documentcompleted被激發一次。另外還有一點需要注意的是,當我在onChange之後延遲System.Threading.Thread.Sleep(5000);該線程只是等待完成回發5秒,它不工作..任何其他建議? – dreampowder 2011-03-02 15:01:55

0

謝謝你。我一直在尋找解決類似問題的日子...... 在我的情況下,我有一個下拉菜單,列表中的項目在「onchange」事件中更新。調用_ _doPostBack更新WebBrowserReadyState允許我在等待「onchange」事件完成之前完成新的下拉列表值。

+0

你很好,我很高興我能幫上忙 – dreampowder 2013-12-23 11:59:22

相關問題