加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 用于查找包含CSS类的祖先节点的XPath

发布时间:2020-12-14 16:19:33 所属栏目:Java 来源:网络整理
导读:我正在写一些Selenium测试,我需要能够找到我已经找到的WebElement的祖先. 这是我正在尝试但没有返回任何结果 // checkbox is also a WebElementWebElement container = checkbox.findElement(By.xpath( "current()/ancestor-or-self::div[contains(@class,'x
我正在写一些Selenium测试,我需要能够找到我已经找到的WebElement的祖先.

这是我正在尝试但没有返回任何结果

// checkbox is also a WebElement
WebElement container = checkbox.findElement(By.xpath(
    "current()/ancestor-or-self::div[contains(@class,'x-grid-view')]") );

下面的图像显示了我选择的深蓝色突出显示的div和我想要用箭头指向它的那个.

UPDATE
试过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有效,但很脆弱

../../../../../../../*

解决方法

这应该选择你想要的元素
./ancestor::div[contains(concat(' ',@class,' '),' x-grid-view ')][1]

用简单的英语:在他们班级中有’x-grid-view’的所有祖先div元素中,选择第一个(最接近的)元素.

笔记:

>我将空间作为一种防御措施,以防止部分匹配.> current()是一个XSLT函数,而不是XPath函数.它在XSLT之外没有任何意义.当前节点表示为.在XPath中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读