【发布时间】:2012-04-22 00:39:20
【问题描述】:
我正在编写一些 Selenium 测试,我需要能够找到我已经找到的 WebElement 的祖先。
这是我正在尝试的,但没有返回任何结果
// checkbox is also a WebElement
WebElement container = checkbox.findElement(By.xpath(
"current()/ancestor-or-self::div[contains(@class, 'x-grid-view')]") );
下图显示了我选择的 div 以深蓝色突出显示,以及我想要找到的带有箭头指向的 div。
更新 尝试了prestomanifesto的建议并得到以下错误
[cucumber] org.openqa.selenium.InvalidSelectorException: The given selector ./ancestor::div[contains(@class, 'x-grid-view']) is either invalid or does not result in a WebElement. The following error occurred:
[cucumber] [InvalidSelectorError] Unable to locate an element with the xpath expression ./ancestor::div[contains(@class, 'x-grid-view']) because of the following error:
[cucumber] [Exception... "The expression is not a legal expression." code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)" location: "file:///C:/Users/JUAN~1.MEN/AppData/Local/Temp/anonymous849245187842385828webdriver-profile/extensions/fxdriv
更新 2 真的很奇怪,即使通过 ID 也不起作用
[cucumber] org.openqa.selenium.NoSuchElementException: Unable to locate element:{"method":"xpath","selector":"./ancestor::div[@id='gridview-1252']"}
更新 3
以下 XPATH 有效,但很脆弱
../../../../../../../*
【问题讨论】:
-
你试过用“./”代替“current()/”吗?
-
@prestomanifesto 我刚刚做了,仍然没有得到任何结果...添加了错误消息
-
该表达式完全合法。出于调试目的,请尝试
ancestor::div[1]、ancestor::div[@id='gridview-1252'][1]等,这将有助于定位中断点。 -
@Tomalak:一定是 selenium 的 xpaths。我尝试了您的建议,但仍然没有找到任何元素,除了我在 Update 3 中显示的内容
-
@downvoter 请通过评论帮助改进问题