Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatiN clicking a button

Tags:

c#

watin

I'm learning the WatiN API and I got to a problem.

I use the WatiN api with the WebBrowser object/control and it work great for most part. im trying to do a search in google with the API but the Button doesn't click as it should.

I'm even using the same code from WatiN site (with small difference): my code:

//need to use a thread to work with the WebBrowser object
var thread = new Thread(() =>
{
    Settings.AutoStartDialogWatcher = false;

    using (IE browser = new IE(webBrowser1.ActiveXInstance))
    {
         browser.GoTo("http://www.google.co.il");
         browser.TextField(Find.ByName("q")).Value = " woowow";
         browser.Button(Find.ByName("btnG")).Click();
    }
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

I can set the text that I'm adding but, the button "btnG" doesn't get clicked.

When i used this code without the WebBrowser object/control the code works fine.

like image 844
samy Avatar asked Mar 08 '26 03:03

samy


1 Answers

i found the problem.

i needed to change focus to the site.

this line of code works:

 browser.NativeDocument.Body.SetFocus();

so now the code look like this:

Settings.AutoStartDialogWatcher = false;

            browser = new IE(webBrowser1.ActiveXInstance);

                browser.GoTo("http://www.google.co.il");
                browser.TextField(Find.ByName("q")).Value = " woowow";

                browser.NativeDocument.Body.SetFocus(); // set focus befor u click

                browser.Button(Find.ByName("btnG")).Click();


        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
like image 197
samy Avatar answered Mar 09 '26 17:03

samy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!