我需要在此HTML中找到输入框:
<div id="employeesDataTable_filter" class="dataTables_filter">
<label>
<input type="search" class="form-control input-sm"
placeholder="Filter..." aria-controls="employeesDataTable">
</label>
</div>
但是对于我的生命来说,不能-请帮忙,
我已经成功地编写了测试袋,并发现了许多不同类型的页面元素,但这使我很困惑。
我对此很陌生并且尝试过
By ExecutiveSearchBox = By.XPath("//input[@type='search' and
class='dataTables_filter']");
答案 0 :(得分:0)
您遇到了问题,因为您正在class
节点上而不是input
上选择div
属性。尝试使用以下选择器:
//div[@class='dataTables_filter']//input[@type='search']
@Marco Forberg也提到过,如果为元素提供了多个类,最好使用contain()
XPath函数:
//div[contains(@class, 'dataTables_filter')]//input[@type='search']
希望它能帮助您解决问题:)
答案 1 :(得分:0)
要在您的 html代码段中查找输入元素,只需使用
FindElement( By.CssSelector( "input" ) )
但请注意:
我总是等待我期望的DOM状态,只有在一段时间未达到该状态后,我才会抛出该状态。
答案 2 :(得分:-2)
要在所需元素内定位并发送字符序列,请为所需的 ElementToBeClickable 诱导 WebDriverWait ,并且您可以使用以下Locator Strategies作为解决方案:
CssSelector
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.form-control.input-sm[aria-controls='employeesDataTable']"))).SendKeys("John Hyslop");
XPath
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='form-control input-sm' and @aria-controls='employeesDataTable']"))).SendKeys("John Hyslop");