Xpath单击某个td行中包含特定文本vlaue的按钮 [英] Xpath clicking button within a td where row contains a specific text vlaue

查看:489
本文介绍了Xpath单击某个td行中包含特定文本vlaue的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用Nightwatch进行一些自动化测试,但CSS选择器非常复杂,并没有为我完成这项工作。我已经开始看XPath来完成这项工作,但是所讨论的表格相当复杂。



我希望能够在一个td值中使用.click()按钮,其中表中的特定行包含特定值。表格如下所示:

 用户名电子邮件显示名称要点击的按钮(想要的第一个)
test test @ example test(1st button)(2nd button)
test2 test2 @ example test(1st button)(2nd button)

每个值都在tr> td之内,因此能够发现它很困难。这是我目前的XPath:

  .click('/ table [@ id =admin-user-list] / tbody / tr [td =test2] / td / button')



 < div id> 
< div class>
< table class>
< tbody>
< tr>
< td data-bind>(用户名)
< td数据绑定>(电子邮件)
< td数据绑定>(显示名称)
< td按钮>
(1st button)
(2nd button)
< / tr>
< / tbody>

每一行都有自己的tr,里面有tds。



一些帮助将不胜感激:)

解决方案

.click('// table [@ id =admin-user-list] / tbody / tr [./ td [text()='test2'] / td / button')




logic: // table [@ id = admin / user / list] / tbody / tr [./ td [text()='test2']
- tr with td has text





但实际上,这并不是一个好主意,因为你在每一列中搜索该值。更好地使用columnName +值的组合

让我们来看看该表的示例: https://www.w3schools.com/css/css_table.asp



我们将搜索表格数据按名称列,例如表 column = Country,data = UK

  // * [@ id中= '客户'] // TR / TD [计数(// * [@ id中= '客户'] //第[文本()= '国家'] /前同辈:: * )+1] [text()='UK'] 

逻辑很简单:
常规定位器是:
// * [@ id ='customers'] // tr / td - 我们正在搜索表格数据
参数: [text()='UK'] 和position =相同,如列名 [count(column_position)]

如何获取列位置:
只需获得所需文本的列:

// * [@ id ='customers'] // th [text()='Country'] 并将其计为pr eceding兄弟姐妹:
// * [@ id ='customers'] // th [text()='Country'] / preceding-sibling :: * ,我们也应该加上+1,因为我们需要当前元素的位置。并计算该工作人员,所以这里是结果: [count(// * [@ id ='customers'] // th [text()='Country'] / preceding-sibling :: * )+1]



所以在列位置我们可以得到一般定位符:


tableId = customers; columnName =国家; dataText = UK;

// * [@ id ='tableId'] // tr / td [count(// * [@ id ='tableId'] // th [text()='columnName'] / preceding-sibling :: *)+ 1] [text()='dataText']


这里是通过data + columnName获取空洞行的定位器

  // * [@ id中= '客户'] // TR [./ TD [计数(// * [@ id中= '客户'] //第[文本()= '国家'] / preceding- sibling :: *)+ 1] [text()='UK']] 

可以搜索其中的任何内容,例如,只需在最后添加
/ td / button - 以从中获取按钮


I'm currently using Nightwatch to do some automated tests, but CSS selectors were extremely complex and did not do the job for me. I've instead started to look at XPath to do the job however the table in question is fairly complex.

I want to be able to .click() a button within a td value, where that specific row in the table contains a specific value. The table looks like this:

Username    Email           Display Name    Buttons to Click (1st one wanted)
test        test@example    test            (1st button)(2nd button)
test2       test2@example   test            (1st button)(2nd button)

Each of these values are within a tr > td so being able to find it is proving difficult. This is my currently XPath:

.click('/table[@id="admin-user-list"]/tbody/tr[td = "test2"]/td/button')

The HTML tree looks like this:

<div id>
    <div class>
       <table class>
           <tbody>
               <tr>
                   <td data-bind>(username)
                   <td data-bind>(email)
                   <td data-bind>(display name)
                   <td button>
                       (1st button)
                       (2nd button)
               </tr>
           </tbody>

Each row has its own tr with those exact tds inside.

Some help would be appreciated :)

解决方案

.click('//table[@id="admin-user-list"]/tbody/tr[./td[text()='test2']/td/button')

logic: //table[@id="admin-user-list"]/tbody/tr[./td[text()='test2'] - tr with td that has text

/td/button -button in that row

but actually this is not really good idea to do that, as you are searching for that value in each column. Better to use combination of columnName+value

Let's check on that table sample: https://www.w3schools.com/css/css_table.asp

We'll search for table data in column by name, for example table column = Country, data = UK

//*[@id='customers']//tr/td[count(//*[@id='customers']//th[text()='Country']/preceding-sibling::*)+1][text()='UK']

again, logic is simple: general locator is: //*[@id='customers']//tr/td - we are searching for table data with parameters: [text()='UK'] and position = same, as in column name [count(column_position)]

How to get column position: just get column with needed text:

//*[@id='customers']//th[text()='Country'] and count it's preceding siblings: //*[@id='customers']//th[text()='Country']/preceding-sibling::* , also we should add +1 , as we need current element's position. and count that staff, so here is the result: [count(//*[@id='customers']//th[text()='Country']/preceding-sibling::*)+1]

so having column position we can get general locator:

tableId = customers; columnName= Country; dataText = UK;

//*[@id='tableId']//tr/td[count(//*[@id='tableId']//th[text()='columnName']/preceding-sibling::*)+1][text()='dataText']

And here is locator to get hole row by data+columnName

//*[@id='customers']//tr[./td[count(//*[@id='customers']//th[text()='Country']/preceding-sibling::*)+1][text()='UK']]

and basically you can search anything inside of it, for example just add in the end /td/button - to get button from it

这篇关于Xpath单击某个td行中包含特定文本vlaue的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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