首页 > 解决方案 > 无法使用相对 xpath 找到元素

问题描述

我目前正在尝试使用 xpath 为我的 selenium 项目定位元素,为什么当我尝试运行 selenium 时,即使它是 DOM 独有的,它也无法使用相对路径定位元素?我的目标是使用 div 标签的 title 属性来定位元素,因为 div 标签是非常动态的。

绝对路径

/html[1]/body[1]/div[2]/div[8]/div[1]/div[3]/div[5]

唯一的相对路径

//*//div[@title='This is the title ']

标签: javaseleniumgoogle-chromexpath

解决方案


你试过用

//*[@title='This is the title ']

代替

//*//div[@title='This is the title ']

实际上,//*直接在所有子节点中定位元素。


推荐阅读