How to use watin with WebBrowser control?

前端 未结 3 1100
难免孤独
难免孤独 2020-12-06 22:18

Is there any way which would allow me to use watin\'s functionalities from my web browser control? simply i want to use watin with my web browser control i don\'t want my ap

3条回答
  •  甜味超标
    2020-12-06 23:04

    If you are using .Net WebBrowser Control you can create WatiN's IE object by using following code:

    var ie = new IE(webBrowser.ActiveXInstance);
    

    But if you do that inside your Form_Load ActiveXInstance will be null. And if you do that for example inside some kind of button_Click the application will hang after you use eg. ie.GoTo. You need to start new thread and operate there. For example:

    private void Form1_Load(object sender, EventArgs e)
    {
        var t = new Thread(() =>
        {
            Settings.AutoStartDialogWatcher = false;
            var ie = new IE(webBrowser1.ActiveXInstance);
            ie.GoTo("http://www.google.com");
        });
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }
    

    You need to disable auto start of dialog watcher, because you cannot use built in WatiN dialog watcher. But with a little bit of hacking you can create your own based on original DialogWatcher class. Handling popups and creating new web browser controls are also possible.

提交回复
热议问题