gpt4 book ai didi

javascript - 根据内容选择表格中的一行并单击所选行中的链接 - Protractor

转载 作者:行者123 更新时间:2023-11-29 21:16:50 26 4
gpt4 key购买 nike

我有一个如下所示的页面对象:

<table border>
<th>Email</th>
<th>action</th>
<tr current-page="adminUsers.meta.page">
<td>admin@example.com</td>
<td><a href="" ng-click="adminUsers.onRemove(user, adminUsers.meta.page)">Delete permanently</a></td>
</tr>
<tr current-page="adminUsers.meta.page">
<td>matilda@snape.com</td>
<td><a href="" ng-click="adminUsers.onRemove(user, adminUsers.meta.page)">Delete permamently</a></td>
</tr>
</table>

我想创建一种方法,使我能够根据用户的电子邮件地址删除用户。

这是我想出的,基于 How to find and click a table element by text using Protractor? :

describe('Admin panel', function() {

it('admin deletes a user', function() {

var re = new RegExp("matilda@snape.com");
var users_list = element.all(by.xpath("//tr[@current-page='adminUsers.meta.page']"));
var delete_btn = element(by.xpath("//a[contains(text(), 'Delete permamently')]"));

users_list.filter(function(user_row, index) {
return user_row.getText().then(function(text) {
return re.test(text);
});
}).then(function(users) {
users[0].delete_btn.click();
});
// some assertion, not relevant right now
});
});

首先,我尝试过滤其中有我要删除的用户的行(所有行都符合我的过滤条件的数组,然后选择第一行 - 无论如何应该是一行),然后单击相应的删除按钮。

但是,根据我的调试,我知道该方法会忽略过滤并单击表中可用的第一个删除按钮,而不是过滤元素中的第一个。我做错了什么?

最佳答案

在这种特殊情况下,我会使用 XPath 及其 following-sibling轴:

function deleteUser(email) {
element(by.xpath("//td[. = '" + email + "']/following-sibling::td/a")).click();
}

关于javascript - 根据内容选择表格中的一行并单击所选行中的链接 - Protractor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39047750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com