简体   繁体   中英

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.

Do you know how I can click the edit button for the 10. person in the table for example?

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. That is because we use Wicket, and it generates dynamic Ids in this situation.

What I did was to get Wicket to add a name attribute, and have Selenium select on this attribute instead of id.

You can use CSS selectors and use the nth-child attribute. For example on the stackoverflow main page you can select the second question with the following CSS selector:

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

You can also use XPath. The XPath for the same element would be:

//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. Each ID should be unique to a page. You should probably remove the ID if you can't make it unique.

What you need sounds like it could be accomplished by using 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. There's a lot you can do with Xpath.

For more info on Xpath see eg the Xpath tutorial here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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