用于返回列索引的 XPath [英] XPath For Returning the Column Index

查看:17
本文介绍了用于返回列索引的 XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 html:

<html>
  <head>
    <body>
      <table>
       <tr>
      <td>Something Else</td>
      <td>Something</td>
    </tr>
    <tr></tr>
  </table>
</body>

第一行第二列的 xpath 表达式为:

The xpath expression for the first row second column is:

/html/body/table/tbody/tr/td[2]

我想使用 selenium 来获取值2".

I'd like to use selenium to get the value '2'.

换句话说,文本Something"第一次出现的值是多少.我能够让 selenium 识别该元素但不返回值.

In other words, what is the value for the first occurrence of text "Something". I'm able to get selenium to recognize that element but not return the value.

到目前为止,一些很好的答案.但是让我澄清一下,这更像是一个硒问题,而不是一个 xpath 问题.我需要弄清楚如何让 selenium 返回这个值.

So far some great answers. But let me clarify that this is more of a selenium question as opposed to a xpath question. I need to figure out how to get selenium to return this value.

推荐答案

以下表达式返回包含所需文本的第一个单元格的位置(列索引):

The following expression returns the position (column index) of the first cell containing the desired text:

count(/html/body/table/tr/td[.='Something'][1]/preceding-sibling::td) + 1

这篇关于用于返回列索引的 XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆