簡體   English   中英

c#webBrowser.Document:回發后重新加載頁面

[英]c# webBrowser.Document: reloading page after postback

我正在開發一個簡單的應用程序,它自動瀏覽包含兩個下拉菜單和一個按鈕的頁面。 該頁面如下所示:

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

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

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

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

我在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中生成的新值?

我通過在引發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;
        }
     }

這次真是萬分感謝。 我一直在尋找類似問題的解決方案......在我的情況下,我有一個下拉菜單,列表中的項目在“onchange”事件期間更新。 調用_ _doPostBack更新WebBrowserReadyState讓我的方式等待“的onchange”事件刮新的下拉列表中值之前完成。

我懷疑您遇到的問題是因為您編寫的代碼不會等待回發發生。 那么會發生什么......

|---> 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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM