1
还是硒和css/xpath定位器的新增功能。我偶然发现了一个CSS可以工作的问题,但是XPath不支持,我真的很想知道为什么。我在示例中使用了Scala,但它仍然是普通的Java Selenium2库。我也用FirefoxDriver硒中的css和等效xpath之间的区别2.0
这里是HTML的有趣的部分:
...
<li class="k-item k-filter-item k-state-default k-last" role="menuitem" style="z-index: auto;">
...
<form class="k-filter-menu k-secondary">
<div>
<div class="k-filter-help-text">Show items with value that:</div>
<span class="k-widget k-dropdown k-header" style="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="" aria-disabled="false" aria-readonly="false" aria-busy="false">
<span class="k-widget k-datetimepicker k-header" style="">
<div>
<button class="k-button" type="submit">Filter</button>
<button class="k-button" type="reset">Clear</button>
</div>
</div>
</form>
</li>
...
我得到李与
val filter = driver.findElement(By.cssSelector("li.k-filter-item"))
这对我的作品。
然后,我想找到按钮。这是一个动态菜单啄滑出并没有什么,所以我需要等待它出现:
new WebDriverWait(driver, selectorTimeout).until(
new ExpectedCondition[Boolean] {
override def apply(d: WebDriver) = {
filter.findElement(By.cssSelector("button[type=submit]")).isDisplayed
}
})
这工作得很好,太。我的问题是,为什么不xpath等效工作:
new WebDriverWait(driver, selectorTimeout).until(
new ExpectedCondition[Boolean] {
override def apply(d: WebDriver) = {
filter.findElement(By.xpath("//button[@type='submit']")).isDisplayed
}
})
任何人?
[编辑]
硒版本:2.35.0
FireFox的驱动程序:2.35.0
我现在使用Opera试试。
你得到的任何错误?你可以尝试'//按钮[包含(@ type,'submit')]'? –
哇。你有没有在FF以外的浏览器中试过它?这应该肯定有效。虽然使用CSS选择器显然更好,但我理解你的困惑。这也让我感到困惑。这是哪个硒版本?哪个Firefox?在Firefox中,CSS选择器和XPath支持都应该是本地的。呵呵。 –
你也需要XPath前面的'.'我想:'。按钮[@type ='submit']'。也可以尝试更详细的:'.//decendant :: button [@type ='submit']'。 (没有测试过这些,但这是一个猜测)。 – Arran