Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >根据表头为表数据创建XPATH

根据表头为表数据创建XPATH
EN

Stack Overflow用户
提问于 2018-02-02 07:35:41
回答 2查看 1.9K关注 0票数 1

我有一个类似于下面的表:table with header and data

在某些情况下,可能会缺少某些列,因此我们为获取表数据而创建的XPath将获取不正确的数据。

例如:如果我需要获取总收入,Xpth将是

.//*[@id='sessionForm:dataTable_data']/tr/td[7]/div

但是假设"Total Revenue“字段之前的一列丢失了,那么XPath将更改为.//*[@id='sessionForm:dataTable_data']/tr/td[6]/div,但在我的脚本中,它将为不正确的列提供值。

表格后面的HTML如下所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<div class="ui-datatable-scrollable-body" tabindex="-1" style="margin-right: 0px; width: 680px;">
 <table role="grid">
   <thead class="ui-datatable-scrollable-theadclone" style="height: 0px;">
     <tr role="row">
      <th id="sessionForm:dataTable:userid_clone" class="ui-state-default ui-sortable-column" style="width:58px" aria-label="User Id: activate to sort column ascending" role="columnheader" tabindex="0" aria-sort="other">
        <span class="ui-column-title">User Id</span>
      </th>
      <th id="sessionForm:dataTable:Enterprisename_clone" class="ui-state-default ui-sortable-column" style="width:70px" aria-label="Enterprise: activate to sort column ascending" role="columnheader" tabindex="0" aria-sort="other">
        <span class="ui-column-title">Enterprise</span>
      </th>
      <th id="sessionForm:dataTable:startTime_clone" class="ui-state-default ui-sortable-column" style="width:56px" aria-label="Start Time: activate to sort column ascending" role="columnheader" tabindex="0" aria-sort="other">
        <span class="ui-column-title">Start Time</span>
      </th>
      <th id="sessionForm:dataTable:endTime_clone" class="ui-state-default ui-sortable-column" style="width:60px" aria-label="End Time: activate to sort column ascending" role="columnheader" tabindex="0" aria-sort="other">
        <span class="ui-column-title">End Time</span>
      </th>
      <th id="sessionForm:dataTable:dynamicColumns:1" class="ui-state-default ui-sortable-column" style="width:80px" aria-label="Sale Amt: activate to sort column ascending" role="columnheader" aria-sort="other">
        <span class="ui-column-title">Sale Amt</span>
        <span class="ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s"/>
      </th>
      <th id="sessionForm:dataTable:dynamicColumns:2" class="ui-state-default ui-sortable-column" style="width:80px" aria-label="Session ID: activate to sort column ascending" role="columnheader" aria-sort="other">
        <span class="ui-column-title">Session ID</span>
        <span class="ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s"/>
      </th>
    </tr>
 </thead>
<tbody id="sessionForm:dataTable_data" class="ui-datatable-data ui-widget-content">
<tr class="ui-widget-content ui-datatable-even" role="row" data-ri="0">
<td role="gridcell">
<span title="test_user@mailinator.com1044">test_user@test.com</span>
</td>
<td role="gridcell">
<span title="test_enterprise">test_enterise</span>
</td>
<td role="gridcell">
<div class="numeric">01/29/2018 16:00:00 </div>
</td>
<td role="gridcell">
<div class="numeric">01/29/2018 21:00:00 </div>
</td>
<td role="gridcell">
<span title="0.0">0.0</span>
</td>
<td role="gridcell">
<span title="41">41</span>
</td>
</tr>
<tr class="ui-widget-content ui-datatable-odd" role="row" data-ri="1">
<td role="gridcell">
<span title="abc_user@mailinator.com2754">abc_user@test.com</span>
</td>
<td role="gridcell">
<span title="abc_enterprise">abc_enterprise</span>
</td>
<td role="gridcell">
<div class="numeric">01/23/2018 14:30:00 </div>
</td>
<td role="gridcell">
<div class="numeric">01/23/2018 15:45:00 </div>
</td>
<td role="gridcell">
<span title="0.0">0.0</span>
</td>
<td role="gridcell">
<span title="40">40</span>
</td>
</tr>

这是部分HTML代码,仅供参考。

我需要根据表头为表列创建XPath,即首先读取表头,然后根据表头上的位置获取该列的所有值。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-02 08:04:44

您可以使用类似以下内容:

.//*[@id='sessionForm:dataTable_data']/tr/td[position()=count(//span[@class='ui-column-title' and text()='Total Revenue']/../preceding-sibling::*)+1]/div

在此xpath中,它将获取Total Revenue的列数,然后使用它来获取列值。因此,假设一列在它之前消失了,那么该值将自动减少1。

希望这能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2018-02-02 10:29:16

Java示例:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// List of title columns
List<WebElement> columns = driver.findElements(By.xpath(".//span[@class='ui-column-title']"));
int i = 1;
boolean stop = False;
WebElement elem = null;
while(i<columns.size() && !stop){
    elem = columns.get(i-1);
    // if the elem has the title stop the loop
    if (elem.getText().equalsIgnoreCase("Total Revenue")){
        stop = True;
    }
}
// The i variable has the right position to take the values for the column
List<WebElement> Revenue_rows = driver.findElements(By.xpath('.//td[(position() > (i-1)) AND (position()<= i)]'))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48577834

复制
相关文章
前端基础-json-server与axios
一个项目从立项开始,一般都是前后端同时进行编码工作的,而此时前端需要的接口和数据后台都是无法提供的;
cwl_java
2020/03/26
9100
【axios】使用json-server 搭建REST API
1.1 API 的分类 REST API: restful (Representational State Transfer (资源)表现层状态转化) (1) 发送请求进行CRUD 哪个操作由请求方式来决定 (2) 同一个请求路径可以进行多个操作 (3) 请求方式会用到GET/POST/PUT/DELETE 非REST API: restless (1) 请求方式不决定请求的CRUD 操作 (2) 一个请求路径只对应一个操作 (3) 一般只有GET/POST 1.2 使用json-server 搭建RES
玖柒的小窝
2021/10/05
2.9K0
【axios】使用json-server 搭建REST API
axios知识盲点整理
JsonServer主要的作用就是搭建本地的数据接口,创建json文件,便于调试调用,模拟和后端服务器进行数据的交互
大忽悠爱学习
2021/11/15
4.1K0
一个很大的变化|将Kubernetes支持窗口增加到一年
从Kubernetes 1.19开始,Kubernetes版本的支持窗口将从9个月增加到1年。较长的支持窗口旨在允许组织在一年中的最佳时间执行主要升级。
CNCF
2020/09/04
4840
一个很大的变化|将Kubernetes支持窗口增加到一年
前端基础-重构TodoList案例
修改:<button @click.stop="removeTodo(key,val.id)" class="destroy"></button>
cwl_java
2020/03/26
4660
上手玩一下json-server(二)操作数据篇——POST/PATCH/DELETE
在上一篇中,我们有db.json文件,里面放置了一些水果信息。 现在新建一个demo文件夹,引入jq库文件(常见的是jquery-2.0.3.min.js,此处的jq.js是被我重命名了)。 另,新建一个jq-ajax.html文件,我们将在这个html文件里头操作db.json数据。
celineWong7
2020/11/05
1.8K0
从零开始学习React-在react项目里面使用mock(七)
从零开始学习React-开发环境的搭建(一) https://www.jianshu.com/p/97f3a1ba168e 从零开始学习React-目录结构,创建组件页面(二) https://www.jianshu.com/p/5b950b8cb73a 从零开始学习React-属性绑定(三) https://www.jianshu.com/p/2c251795d1b3 从零开始学习React-路由react-router配置(四) https://www.jianshu.com/p/2b86d5f4d9d7 从零开始学习React-axios获取服务器API接口(五) https://www.jianshu.com/p/81ca5cc94923 从零开始学习React-解析json、渲染数据(六) https://www.jianshu.com/p/1a998147b09b 从零开始学习React-在react项目里面使用mock(七) https://www.jianshu.com/p/2a5f296a865c
王小婷
2019/11/21
1.8K0
axios笔记(一) 简单入门
打开 http://localhost:3000/,可以在 Resources 中看到所有的接口
赤蓝紫
2023/01/05
1.6K0
axios笔记(一)    简单入门
Ajax笔记(2) -Axios
xhr.onreadystatechange = function() { 当事件发生时执行的代码 }
y191024
2022/09/20
1.4K0
Ajax笔记(2) -Axios
前端必备技能:json-server全攻略
由于json-server需要通过Node对其进行启动,所以首先要安装Node。Node可自行安装,在此不再进行演示。
用户1272076
2020/08/28
1.7K0
vue 构建 todo 项目系列 2
vue 构建 todo 项目系列 1 只是用模拟的数据,页面一刷新就打回原型,本文将用 json-server 构建一个简易的 api,模拟真实的数据服务器
章鱼喵
2019/10/16
6620
json-server
在前端与后端开发的时候,偶尔会有在后端接口没有开发完毕时进行一些接口的mock,这里有一款开源框架做了类似的事情,让你可以“不到30秒得到一个零编码的假REST API”
阿超
2023/06/23
1770
json-server
将oracle驱动包加到maven中
2、确认maven环境变量已整确(cmd 中执行 mvn -v 显示maven的版本信息);
qubianzhong
2018/08/10
5060
Vue里面怎么模拟假数据
在 Vue 组件的 data 选项中定义一个对象,作为假数据的容器。在该对象中设置各种属性和初始值来模拟假数据。
王小婷
2023/09/26
3970
一行命令, 静态json变身api
作为一个前端开发者, 你可以会遇到没有测试数据的尴尬, 而这次我们用json-server, 优雅的解决这个问题 效果 关于 json-server js
zhaoolee
2018/06/14
9500
将Sublime添加到鼠标右键
2、找到 HKEY_CLASSES_ROOT/*/shell 目录,在此目录下操作。
新码农
2020/03/05
3K0
Vue+SessionStorage实现简单的登录
我是基于vue脚手架cli做的,没用过cli的可以看下我之前写的cli脚手架搭建
RtyXmd
2018/08/30
11.7K1
Vue+SessionStorage实现简单的登录
React使用axios请求并渲染数据
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中
Leophen
2019/08/23
3.9K0
React-Router-URL参数
React Router 是 React 应用中常用的路由管理库,而 URL 参数则是它的一个关键概念。URL 参数允许您在路由之间传递数据,从而使您的应用程序更灵活和交互性更强。
杨不易呀
2023/10/01
2810
点击加载更多

相似问题

向VBA/VB6集合添加项时出错

41

编译基板添加托盘失败

17

用托盘添加apt回购

16

在基板上添加托盘

111

为自定义托盘基板添加自定义RPC时出错

215
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文