Xpath单击某个td行中包含特定文本vlaue的按钮 [英] Xpath clicking button within a td where row contains a specific text 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
让我们来看看该表的示例: https://www.w3schools.com/css/css_table.asp
我们将搜索表格数据按名称列,例如表 column = Country,data = UK
// * [@ id中= '客户'] // TR / TD [计数(// * [@ id中= '客户'] //第[文本()= '国家'] /前同辈:: * )+1] [text()='UK']
逻辑很简单: 如何获取列位置: 所以在列位置我们可以得到一般定位符: tableId = customers; columnName =国家; dataText = UK; 这里是通过data + columnName获取空洞行的定位器 可以搜索其中的任何内容,例如,只需在最后添加 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: Each of these values are within a tr > td so being able to find it is proving difficult. This is my currently XPath: The HTML tree looks like this: Each row has its own tr with those exact tds inside. Some help would be appreciated :)
logic: 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 again, logic is simple:
general locator is:
How to get column position:
just get column with needed text: so having column position we can get general locator: tableId = customers; columnName= Country; dataText = UK; And here is locator to get hole row by data+columnName and basically you can search anything inside of it, for example just add in the end
这篇关于Xpath单击某个td行中包含特定文本vlaue的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
常规定位器是:
// * [@ id ='customers'] // tr / td
- 我们正在搜索表格数据
参数: [text()='UK']
和position =相同,如列名 [count(column_position)] $ c
只需获得所需文本的列:
// * [@ id ='customers'] // th [text()='Country']
并将其计为pr eceding兄弟姐妹:
// * [@ id ='customers'] // th [text()='Country'] / preceding-sibling :: *
,我们也应该加上+1,因为我们需要当前元素的位置。并计算该工作人员,所以这里是结果: [count(// * [@ id ='customers'] // th [text()='Country'] / preceding-sibling :: * )+1]
// * [@ id ='tableId'] // tr / td [count(// * [@ id ='tableId'] // th [text()='columnName'] / preceding-sibling :: *)+ 1] [text()='dataText']
// * [@ id中= '客户'] // TR [./ TD [计数(// * [@ id中= '客户'] //第[文本()= '国家'] / preceding- sibling :: *)+ 1] [text()='UK']]
/ td / button
- 以从中获取按钮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)
.click('/table[@id="admin-user-list"]/tbody/tr[td = "test2"]/td/button')
<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>
.click('//table[@id="admin-user-list"]/tbody/tr[./td[text()='test2']/td/button')
//table[@id="admin-user-list"]/tbody/tr[./td[text()='test2']
- tr with td that has text/td/button
-button in that rowcolumn = Country, data = UK
//*[@id='customers']//tr/td[count(//*[@id='customers']//th[text()='Country']/preceding-sibling::*)+1][text()='UK']
//*[@id='customers']//tr/td
- we are searching for table data
with parameters: [text()='UK']
and position = same, as in column name [count(column_position)]
//*[@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]
//*[@id='tableId']//tr/td[count(//*[@id='tableId']//th[text()='columnName']/preceding-sibling::*)+1][text()='dataText']
//*[@id='customers']//tr[./td[count(//*[@id='customers']//th[text()='Country']/preceding-sibling::*)+1][text()='UK']]
/td/button
- to get button from it