Search code examples
c#watinwebautomation

not able to click button using watin


<div class="flow-right">
    <button type="button" id="btnClear_page" class="uiButton" title="Clear" onmouseover="JSButtonUtils.doBtnOver(this)" onmousedown="JSButtonUtils.doBtnDown(this)" onmouseout="JSButtonUtils.doBtnOut(this)" onclick="if(JSButtonUtils.debounce(this, 1200)){ return false } else { return btnClear_page_click(this, true, true);}  ">
      <div class="uiButton-content">
        <div class="uiButton-label">Clear</div>
      </div>
    </button>
    <button type="button" id="btnSearch_page" class="uiButton primary" title="Search" onmouseover="JSButtonUtils.doBtnOver(this)" onmousedown="JSButtonUtils.doBtnDown(this)" onmouseout="JSButtonUtils.doBtnOut(this)" onclick="if(JSButtonUtils.debounce(this, 1200)){ return false } else { return btnSearch_page_click(this, true, true);}  ">
      <div class="uiButton-content">
        <div class="uiButton-label">Search</div>
      </div>
    </button>
</div>

I have above html code, button wrapped inside Div, I have tried to find the button and click on it, no luck and also tried clicking over Div no luck. Using IE9 browser and C#.

using (var browser = new IE("https://Site.com"))
            {
     browser.Div(Find.ByText("Search")).Click();
      browser.Button(Find.ById("btnSearch_page")).Click();
}

Solution

  • I have had flaky experiences with WatiN in the past and I have always used guard clauses in my WatiN code to fend off NullReferenceExceptions, like this:

    using (var browser = new IE("https://Site.com"))
    {
        var SearchDiv = browser.Div(Find.ByText("Search"));
        // Only try to click the DIV if we found it
        if(null != SearchDiv)
        {
            SearchDiv.Click();
        }
    
        var SearchButton = browser.Button(Find.ById("btnSearch_page"));
        // Only try to click the button if we found it
        if(null != SearchButton)
        {
            SearchButton.Click();
        }
    }