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();