C# WebBrowser 按钮单击,然后转到另一个页面

发布于 2024-12-12 07:12:41 字数 626 浏览 2 评论 0原文

我需要单击 html 按钮并导航到另一个页面。单击后我需要等待页面加载,只有当旧页面加载时才转到新页面。

这是单击按钮的代码:

element = webBrowser1.Document.GetElementById("LoginButton");
element.InvokeMember("click");

webBrowser 有一个 IsBusy 属性,但单击按钮后不起作用:

element = webBrowser1.Document.GetElementById("LoginButton");
element.InvokeMember("click");
if(webBrowser1.IsBusy)
{
     MessageBox.Show("Busy"); // Nothing happens, but page is not full loaded.
}

如果我添加 System.Threading.Thread.Sleep(1000) 页面将加载我可以转到下一页,但其他计算机上的页面加载时间可能会更长。

如何才能仅在加载上一个页面后才加载另一个页面?

PS:我来自俄罗斯,很抱歉英语不好。

I need to click an html button and navigate to another page. After click I need to wait for page loading, and go to the new page only when the old page loaded.

Here is the code, that click a button:

element = webBrowser1.Document.GetElementById("LoginButton");
element.InvokeMember("click");

webBrowser has got a IsBusy property, but it don`t works after button click:

element = webBrowser1.Document.GetElementById("LoginButton");
element.InvokeMember("click");
if(webBrowser1.IsBusy)
{
     MessageBox.Show("Busy"); // Nothing happens, but page is not full loaded.
}

If I add System.Threading.Thread.Sleep(1000) the page loads and I can go to next page, but page loading time on other computers can be more.

What can I do to load another page only after the previous page has loaded?

P.S: I am from Russia, so sorry for bad English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清晨说晚安 2024-12-19 07:12:42

如果您的网页有任何 javascript 块,您将无法使用 WebBrowser 控件本身解决问题。您应该使用 javascript 代码等待 document.ready 事件,并让您的 C# 程序了解该事件。

之前,我制作了一个提供网页状态的 JavaScript 块。它看起来像这样:

var isBusy = true;
function getIsScriptBusy () {
   return isBusy;
}
// when loading is complete:
// isBusy = false;
// document.ready event, for example

以及等待它返回 true 的 C# 代码:

void WaitForCallback(int timeout) {
    Stopwatch w = new Stopwatch();
    w.Start();
    Wait(delegate() {
        return (string)Document.InvokeScript("getIsScriptBusy") != "false"
            && (w.ElapsedMilliseconds < timeout || Debugger.IsAttached);
    });
    if(w.ElapsedMilliseconds >= timeout && !Debugger.IsAttached)
        throw new Exception("Operation timed out.");
}
void Wait(WaitDelegate waitCondition) {
    int bRet;
    MSG msg = new MSG();
    while(waitCondition() && (bRet = GetMessage(ref msg, new HandleRef(null, IntPtr.Zero), 0, 0)) != 0) {
        if(bRet == -1) {
            // handle the error and possibly exit
        } else {
            TranslateMessage(ref msg);
            DispatchMessage(ref msg);
        }
        Thread.Sleep(0);
    }
}

If your webpage has any javascript blocks, you won't be able to solve the problem using the WebBrowser control itself. You should wait for a document.ready event using javascript code and let know your C# program about it.

Previously, I made a javascript block that provides the webpage state. It looks like this:

var isBusy = true;
function getIsScriptBusy () {
   return isBusy;
}
// when loading is complete:
// isBusy = false;
// document.ready event, for example

and a C# code that waits for it to return true:

void WaitForCallback(int timeout) {
    Stopwatch w = new Stopwatch();
    w.Start();
    Wait(delegate() {
        return (string)Document.InvokeScript("getIsScriptBusy") != "false"
            && (w.ElapsedMilliseconds < timeout || Debugger.IsAttached);
    });
    if(w.ElapsedMilliseconds >= timeout && !Debugger.IsAttached)
        throw new Exception("Operation timed out.");
}
void Wait(WaitDelegate waitCondition) {
    int bRet;
    MSG msg = new MSG();
    while(waitCondition() && (bRet = GetMessage(ref msg, new HandleRef(null, IntPtr.Zero), 0, 0)) != 0) {
        if(bRet == -1) {
            // handle the error and possibly exit
        } else {
            TranslateMessage(ref msg);
            DispatchMessage(ref msg);
        }
        Thread.Sleep(0);
    }
}
巡山小妖精 2024-12-19 07:12:42

WebBrowser 控件公开了许多事件。您可以尝试 NavigedDocumentCompleted

缺口

There are lots of events exposed by the WebBrowser control. You might try Navigated or DocumentCompleted.

Nick

无所谓啦 2024-12-19 07:12:42

WebBrowser.Navierated浏览器事件

WebBrowser.Navigated is the browser event you are seeking.

傲鸠 2024-12-19 07:12:42

用这个,
您可能只能使用一次

 br1.DocumentCompleted += br1_DocumentCompleted;
        Application.Run();

调用

void br1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var br1 = sender as WebBrowser;
        if (br1.Url == e.Url)
        {
            Console.WriteLine("Natigated to {0}", e.Url);
            Application.ExitThread();   // Stops the thread
        }
    }

将 br1 替换为您的网络浏览器名称
希望这有帮助

Use this ,
You might just be able to use this once

 br1.DocumentCompleted += br1_DocumentCompleted;
        Application.Run();

Call

void br1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var br1 = sender as WebBrowser;
        if (br1.Url == e.Url)
        {
            Console.WriteLine("Natigated to {0}", e.Url);
            Application.ExitThread();   // Stops the thread
        }
    }

Replace br1 with your webbrowser name
Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文