webbrowser DocumentText不会在表单提交时更新

本文关键字:表单提交 更新 DocumentText webbrowser | 更新日期: 2023-09-27 18:26:16

我有一个网站,它以用户名作为输入并显示一些数据。任务是创建一个可以执行以下步骤的应用程序:1.打开网站。2.输入使用详细信息并提交表格。3.检索数据。我正在使用webBrowser控件与web元素进行交互。这是代码:

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.AllowNavigation = true;
        // Supress the java script errors on the screen 
        webBrowser1.ScriptErrorsSuppressed = true;
        bool complete = false;
        this.webBrowser1.DocumentCompleted += delegate
        {
            if (complete)
                return;
            complete = true;
            this.webBrowser1.Document.Window.AttachEventHandler("onload", delegate
            {
                System.Threading.SynchronizationContext.Current.Post(delegate
                {
                    var elemList = GetElementsByIdNameContains(webBrowser1, "input", "txtEmpNo");
                    var inputBox = elemList[0];
                    var emp_id = "621999";
                    var errorMsg = "Searched Employee " + emp_id + " doesnt not exist.";
                    inputBox.InnerText = emp_id;
                    var elemList1 = GetElementsByIdNameContains(webBrowser1, "input", "btnSubmit");
                    var submitButton = elemList1[0];
                    submitButton.InvokeMember("Click");
        var doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(webBrowser1.DocumentText);
        StringBuilder rowData = new StringBuilder();
        foreach (HtmlAgilityPack.HtmlNode table in doc.DocumentNode.SelectNodes("//table[contains(@id,'_grdvwAttendance')]"))
            foreach (HtmlAgilityPack.HtmlNode row in table.SelectNodes("tr"))
            {
                // TODO: Need to format the output.
                rowData.Clear();
                foreach (HtmlAgilityPack.HtmlNode cell in row.SelectNodes("th|td"))
                {
                    rowData.Append(cell.InnerText + "");
                }
                listBox1.Items.Add(rowData);
            }
                 }, null);
            });
        };
        webBrowser1.Navigate("SomeURl");
    }

调用"Click"后,webbrowser1.DocumentText不会得到更新,代码在foreach行(doc.DocumentNode.SelectNodes中的HtmlAgilityPack.HtmlNode表("//table[contains(@id,'_grdvwAttendance')]")上抛出异常,因为它无法在页面上找到元素。有人能帮忙吗?

webbrowser DocumentText不会在表单提交时更新

问题可能是您试图在内容完成之前(即在post方法返回结果之前)获取提交请求和响应。调用submit"Click"选项后给予一段时间并尝试。否则,您可以检查文档完成状态并检查元素。