3
votes

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.

2
The C# programming language does not have a webbrowser control. - John Saunders
Ha ha, you're funny: System.Windows.Forms.WebBrowser - Corey Ogburn
That's not defined in C# language spec. Strictly speaking this question is related with English too, but you should not tag the question with English either. C# or English cannot help you solve non-linguistic problems. - Sheng Jiang 蒋晟

2 Answers

1
votes

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.

1
votes

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.