简体   繁体   English

如何判断点击了哪个按钮?

[英]How to tell which button was clicked?

I have a table with some entries (names).我有一个包含一些条目(名称)的表。 At each name I have the button "edit".在每个名称上,我都有“编辑”按钮。 My problem is, that all edit buttons have the same id.我的问题是,所有编辑按钮都具有相同的 ID。

Do you know how I can click the edit button for the 10. person in the table for example?例如,您知道如何单击表中 10. person 的编辑按钮吗?

We had a related problem using Selenium, the difference being that we don't know the value of the id attribute for Ajax enabled components.我们在使用 Selenium 时遇到了一个相关问题,不同之处在于我们不知道启用 Ajax 的组件的 id 属性值。 That is because we use Wicket, and it generates dynamic Ids in this situation.那是因为我们使用了 Wicket,它会在这种情况下生成动态 Id。

What I did was to get Wicket to add a name attribute, and have Selenium select on this attribute instead of id.我所做的是让 Wicket 添加一个名称属性,并在此属性上使用 Selenium select 而不是 id。

You can use CSS selectors and use the nth-child attribute.您可以使用 CSS 选择器并使用 nth-child 属性。 For example on the stackoverflow main page you can select the second question with the following CSS selector:例如,在 stackoverflow 主页上,您可以使用以下 CSS 选择器 select 第二个问题:

.container .question-summary:nth-child(2)

You can also use XPath.您也可以使用 XPath。 The XPath for the same element would be:相同元素的 XPath 将是:

//div[@class='question-summary']/div[2]

First thing is, if you have the same ID for every button this is wrong according to the HTML specification.首先,如果每个按钮的 ID 相同,根据 HTML 规范,这是错误的。 Each ID should be unique to a page.每个 ID 对一个页面应该是唯一的。 You should probably remove the ID if you can't make it unique.如果您不能使其唯一,您可能应该删除该 ID。

What you need sounds like it could be accomplished by using XPATH.您需要的听起来可以通过使用 XPATH 来完成。

For example, you could use something like:例如,您可以使用以下内容:

//table[@class='myTableClass']/tr[10]/td[4]/input

If you are searching for a particular value you can use "contains":如果您正在搜索特定值,您可以使用“包含”:

//table[@class='myTableClass']//td[contains(., 'ABC')]../td[5]

What that does is search for a table cell with the value ABC , then select the parent of that cell (the row), then select the 5th cell of that row.这样做是搜索值为ABC的表格单元格,然后搜索 select 该单元格的父级(行),然后 select 该行的第 5 个单元格。 There's a lot you can do with Xpath.您可以使用 Xpath 做很多事情。

For more info on Xpath see eg the Xpath tutorial here .有关 Xpath 的更多信息,请参见 此处的 Xpath 教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM