views:

286

answers:

2

I'm trying to automate a web process where I need to click a button repeatedly. When my code "clicks" that button (an HtmlElement obtained from the WebBrowser control I have on my form) then it brings focus back to my application, more specifically the WebBrowser control. I wish to better automate this process so that the user can do other things while the process is going on, but that can't happen if the window is unminimizing itself because it's attaining focus.

The code associated with the clicking is:

HtmlElement button = Recruiter.Document.GetElementById("recruit_link");
button.InvokeMember("click");

I've also tried button.RaiseEvent("onclick") and am getting the exact same results, with focus problems and all.

I've also tried hiding the form, but when the InvokeMember/RaiseEvent method is called, whatever I was working on loses focus but since the form is not visible then the focus seems to go nowhere.

The only non-default thing about the webbrowser is it's URI being set to my page and ScriptErrorsSuppressed being set to True.

A: 

In IE7 or higher you can implement IProtectFocus on the webbrowser site and deny the focus change. You would be much better off if you use the raw ActiveX or its wrappers that support this kind of customization, e.g. csexwb. If you have to use the winform webbrowser control, you need to create your own webbrowser site.

Sheng Jiang 蒋晟
I'm not using iexplorer.exe or any "real" browser. I'm using the System.Windows.Forms.WebBrowser control out of .Net Framework 4.0 beta.
Corey Ogburn
I am not talking abut other browsers either. webbrowser site is a nested class in System.Windows.Forms.WebBrowser.
Sheng Jiang 蒋晟
+1  A: 

Why do You need to click this button? It's sending some form? If yes, you can use WebClient and simulate sending form without graphic interface.

Basicly almost anything significant requires connection to website using Get Post or multipart/post so WebClient will be perfect. You can simply get the site wich is this button on and parse it to get what clicking it do. And then simulate your own action.

Kaminari