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

2 回答 2

1

过去我在使用 WatiN 方面有过不愉快的经历,我总是在我的 WatiN 代码中使用保护子句来抵御NullReferenceExceptions,如下所示:

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();
    }
}
于 2013-07-23T01:50:13.067 回答
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();
                }
于 2013-07-24T02:45:22.237 回答