我正在编写一些 Selenium 测试,我需要能够找到WebElement
我已经找到的 a 的祖先。
这是我正在尝试但没有返回任何结果
// 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 有效,但很脆弱
../../../../../../../*