UI automation · Build it
A locator for a row in a changing table
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
Here's an orders table. The row ids change on every page load, and sorting changes the row order. Write a Selenium locator, XPath or CSS, that clicks the Cancel button in the row for order ORD-1042. Then tell me which one you'd keep in the framework.
<table id="orders">
<tr id="r-8f3a">
<td>ORD-1041</td><td>Delivered</td>
<td><button class="btn btn-sm">View</button><button class="btn btn-sm danger">Cancel</button></td>
</tr>
<tr id="r-19bc">
<td>ORD-1042</td><td>Packed</td>
<td><button class="btn btn-sm">View</button><button class="btn btn-sm danger">Cancel</button></td>
</tr>
</table>
Then
Follow-ups the interviewer asks next
- Why not use the row's id?
- What's the difference between
text()and.in XPath? - Can CSS do this on its own?
- What if the order number cell has extra spaces or a link inside it?
- What if two tables on the page look the same?
- What would you ask the developers to add?
- How would you do the same in Playwright?
The answer
What a strong answer covers
- XPath:
//table[@id='orders']//tr[td[normalize-space()='ORD-1042']]//button[normalize-space()='Cancel'].normalize-space()copes with extra spaces and nested tags. - Other forms:
//td[normalize-space()='ORD-1042']/ancestor::tr//button[text()='Cancel'], orfollowing-sibling::td/button[...]. - Standard CSS can't select by text (no
:contains); with CSS you'd find all rows and filter in Java, which is slower and wordier. text()matches only a direct text node;.ornormalize-space()uses the whole string value, children included.- Scoped to the table's id, so a second table can't match.
- Lives in the page object as
cancelOrder(String orderNo), which builds the locator; tests never hold raw locator strings. - Best long term: ask for test attributes, such as
data-order="ORD-1042"on the row anddata-testid="cancel"on the button, so the CSS becomestr[data-order='ORD-1042'] [data-testid='cancel']. - Playwright:
page.getByRole('row', { name: /ORD-1042/ }).getByRole('button', { name: 'Cancel' }), orpage.locator('tr').filter({ hasText: 'ORD-1042' }).
The red pen
Common traps
Avoid:the generated row id; index locators like //tr[2]/td[3]/button[2]; styling classes like btn btn-sm danger; contains(text(),'ORD-104'), which also matches ORD-1041 and ORD-10420.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.