xpath:在它之前和之后的元素的上下文中查找元素 [英] xpath: Finding an element in the context of the element before it and after it

查看:36
本文介绍了xpath:在它之前和之后的元素的上下文中查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xpaths 运行使用 VB 在 C# 中开发的自动化.我无法在两个不同元素的上下文中找到一个元素.我需要这样做,因为该元素与页面上的其他元素共享相同的名称,但它后面和前面的元素是唯一的.

我还上传了一个文件来帮助您理解,我想在它之前和之后的上下文中查找日期.

http://www.filedropper.com/example

<头><身体><div>这是一个测试<div><h1>测试1</h1>

<div><h1>日期

<div><h1>时间1</h1>

<div><h1>测试2</h1>

<div><h1>日期

<div><h1>时间2</h1>

</html>

解决方案

应该这样做:

/html/body/div/div/h1[../following-sibling::*[1]/h1/text() = " time2 " and ../preceding-sibling::*[1]/h1/text() = " test2 "]

following-sibling::*[1] 返回第一个跟随的兄弟.由于 div 和 h1 元素是嵌套的,因此您必须先在层次结构中上一层.

I am using xpaths to run automation developed in C# using VB. I am having trouble finding an element in the context of two different elements. I need to do this because the element shares the same name with other elements on the page but the elements following and preceding it are unique.

I have also upload a file to help you understand, I want to find date in the context of whats before it and after it.

http://www.filedropper.com/example

<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>

解决方案

This should do it:

/html/body/div/div/h1[../following-sibling::*[1]/h1/text() = " time2 " and ../preceding-sibling::*[1]/h1/text() = " test2 "]

following-sibling::*[1] return the first following sibling. As the div and h1 elements are nested, you have to go up one level in the hierarchy first.

这篇关于xpath:在它之前和之后的元素的上下文中查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆