我想找到一个xpath元素,并通过检查它前后的元素来确认它是正确的

本文关键字:元素 检查 确认 xpath 一个 | 更新日期: 2023-09-27 18:01:19

我使用xpath来运行在c#中使用VB开发的自动化。我很难找到一个元素,然后通过检查前一个元素和后一个元素来确认元素是正确的。

例子:
我想选择第二个"日期",然后通过检查"test2"之前的元素和"time2"之后的元素是否为正确的元素来确认它是正确的"日期"。明白了吗?

或者:我一直在firefox上使用Firepath来查找该元素。我一直在使用的命令是//前面::[contains(text(),'time2')]//[contains(text(),'date')]//下面::*[contains(text(),'test2')]

我还添加了我的html:
<html>
<head>
</head>
<body>
<div> this is a test 
   <div> <h1> test1 </h1> </div>
   <div> <h1> date </h1> </div>
   <div> <h1> time1 </h1> </div>
   <div> <h1> test2 </h1> </div>
   <div> <h1> date </h1> </div>
   <div> <h1> time2 </h1> </div>
</div>
</body>
</html>

我想找到一个xpath元素,并通过检查它前后的元素来确认它是正确的

这一行是什么?

/html/body/div/div[h1/text()=' date ' and preceding-sibling::div/h1/text()=' test2 ' and following-sibling::div/h1/text()=' time2 ']

或启动root并检查所有,就像您的那样:

//div/div[h1/text()=' date ' and preceding-sibling::div/h1/text()=' test2 ' and following-sibling::div/h1/text()=' time2 ']