πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
Back to All Scenarios
Scenario 46 of 100
Data Handling
Intermediate

Locating and Clicking a Specific Row by Cell Text

πŸ‘€Scenario Overview

Locating and Clicking a Specific Row by Cell Text

Key Takeaways & Cheat Sheet

  • βœ“Construct dynamic XPaths using text matching attributes
  • βœ“Navigate from target cell back to row node using ancestor or parent axes
  • βœ“Locate specific action button within the target row boundary
  • βœ“Avoid hardcoded row index numbers that change dynamically

Short Direct Answer

To interact with an action button (like "Edit" or "Delete") in a row containing specific text (e.g., matching the user "Pooja"), use a relative XPath. Start by locating the cell containing the target text, navigate up to the parent row container, and then locate the action button within that row.

⚠️ Senior Warning (Red Flag)

Do not use index-based selectors (e.g. clicking row 3 button) to click rows matching specific names. If table sorting or dynamic pagination shifts the row positions, your script will click the wrong user.

πŸ’‘ STAR Deep Dive Explanation & Pro Tip

This dynamic XPath approach is highly stable because it binds the click directly to the target record, regardless of alphabetical sorting or table position shifts.

SeleniumAutomation.java
Selenium 4 + Java
// βœ… Smart XPath: Locate row containing 'Pooja', then locate 'Edit' button in that row
String targetUser = "Pooja";
String xpathQuery = "//tr[td[contains(text(),'" + targetUser + "')]]//button[text()='Edit']";

WebElement editBtn = driver.findElement(By.xpath(xpathQuery));
editBtn.click();