Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

.net - How to use watin with WebBrowser control?

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 application to open a new window ,i need it in my web browser control.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...