无法使用watin单击按钮

时间:2013-07-23 00:29:00

标签: c# watin webautomation

<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>

我有上面的html代码,按钮包裹在Div内部,我试图找到按钮并单击它,没有运气也尝试点击Div没有运气。使用IE9浏览器和C#。

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

2 个答案:

答案 0 :(得分:1)

我过去曾经和WatiN有过一些经历,我总是在我的WatiN代码中使用保护条款来抵挡NullReferenceException s,就像这样:

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();
    }
}

答案 1 :(得分:0)

iframe元素被视为不同的表单元素,不属于主要表单。 使用下面的代码,我们可以点击iframe下的所有子元素。

WatiN.Core.Frame frame = browser.Frame(Find.ById("frameMain"));
                var buttons = frame.Button(Find.ById("btnSearch_page"));
                if (buttons.Exists)
                {
                    buttons.Click();
                    browser.WaitForComplete();
                }