Starts-with() in XPath
starts-with() is an XPath function used in Selenium to locate web elements when an attribute value or text begins with a specific string. It is especially useful for handling dynamic web elements where the beginning portion of an attribute remains stable while the remaining portion changes.
XPath is an important locator technique used with Selenium WebDriver. JustAcademy's Selenium training curriculum includes locators such as XPath and CSS selectors as part of Selenium WebDriver and element automation.
For more Selenium learning resources, visit Selenium Training Course and explore the Selenium Course Demo.
1. What is starts-with() in XPath?
The starts-with() function checks whether a string starts with a specified value. In Selenium automation, it can be used to create XPath expressions for elements whose attributes or text begin with a known value.
For example, suppose an input element has a dynamic ID:
<input id="user_12345" type="text">
If the numeric portion of the ID changes dynamically but user_ always remains constant, starts-with() can be used:
//input[starts-with(@id,'user_')]
This XPath can locate the input element without depending on the complete dynamic ID.
2. Basic Syntax of starts-with()
The basic XPath syntax is:
starts-with(string, substring)
When used with an HTML attribute:
//tagname[starts-with(@attribute,'value')]
Example:
//input[starts-with(@id,'user')]
This expression selects an input element whose id attribute starts with user.
3. Understanding the Syntax
| Part | Meaning |
| // | Searches for matching elements anywhere in the document. |
| input | Specifies the HTML element. |
| starts-with() | Checks whether a string starts with a specified value. |
| @id | Refers to the id attribute. |
| 'user' | The expected starting text. |
4. Why is starts-with() Used in Selenium?
Modern web applications frequently generate dynamic attribute values. An element may have an ID such as:
user_1001
user_1002
user_1003
The complete ID changes, but the beginning remains the same. Instead of creating separate XPath expressions for every value, starts-with() can identify the common pattern.
//input[starts-with(@id,'user_')]
5. starts-with() with ID Attribute
The most common use of starts-with() is locating elements using dynamic IDs.
HTML:
<input id="username_98765" type="text">
XPath:
//input[starts-with(@id,'username_')]
This XPath matches an input whose ID begins with username_.
6. starts-with() with Name Attribute
The function can also be used with the name attribute.
<input name="user_login_123" type="text">
XPath:
//input[starts-with(@name,'user_login_')]
This is useful when the name attribute contains a stable prefix and a dynamic suffix.
7. starts-with() with Class Attribute
starts-with() can be applied to the class attribute when the class value has a predictable beginning.
<button class="btn-primary-submit">Submit</button>
XPath:
//button[starts-with(@class,'btn-primary')]
However, class attributes may contain multiple class names. Therefore, class-based XPath expressions should be created carefully.
8. starts-with() with Placeholder
The placeholder attribute can also be used.
<input placeholder="Enter your username here" type="text">
XPath:
//input[starts-with(@placeholder,'Enter your')]
This matches the input because its placeholder starts with Enter your.
9. starts-with() with Type Attribute
starts-with() can technically be used with attributes such as type.
//input[starts-with(@type,'text')]
For static values, however, exact matching is generally simpler:
//input[@type='text']
starts-with() is most useful when the attribute contains dynamic or variable values.
10. starts-with() with Text
starts-with() can be used with element text when the visible text begins with a known value.
//button[starts-with(text(),'Login')]
Example HTML:
<button>Login to Account</button>
The XPath can match the button because its text starts with Login.
11. starts-with() with normalize-space()
Whitespace can sometimes affect text-based XPath expressions. The normalize-space() function can be combined with starts-with() to remove unnecessary leading and trailing whitespace and normalize internal whitespace.
//button[starts-with(normalize-space(text()),'Login')]
This is useful when HTML contains extra whitespace around the visible text.
12. starts-with() with the Dot Operator
The dot . represents the string value of the current node. It is useful when an element contains nested elements.
//button[starts-with(normalize-space(.),'Login')]
Example:
<button>
<span>Login</span>
<span>Now</span>
</button>
Using . can be more reliable than text() when the visible text is distributed across descendant elements.
13. starts-with() with Nested Elements
Consider the following HTML:
<button class="action">
<span>Login</span>
</button>
An XPath using the button's complete string value can be:
//button[starts-with(normalize-space(.),'Login')]
This is useful when the target element contains child elements.
14. Dynamic ID Example
Suppose a web application generates IDs such as:
<input id="email_1001">
<input id="email_1002">
<input id="email_1003">
The stable part is email_.
//input[starts-with(@id,'email_')]
This XPath can identify elements whose ID begins with the specified prefix.
15. Dynamic Button ID Example
Suppose a button has:
<button id="submit_78452">Submit</button>
Use:
//button[starts-with(@id,'submit_')]
The XPath does not depend on the changing numeric suffix.
16. Dynamic Link Example
Suppose links have dynamic href values:
<a href="/products/1001">Product 1</a>
<a href="/products/1002">Product 2</a>
XPath:
//a[starts-with(@href,'/products/')]
This can locate links whose href begins with the specified path.
17. starts-with() with href
The href attribute is frequently useful for starts-with() when URLs share a common beginning.
//a[starts-with(@href,'https://example.com/products')]
This expression targets links whose href starts with the specified URL.
18. starts-with() with aria-label
Modern web applications frequently use accessibility attributes such as aria-label.
<button aria-label="Search products">🔍</button>
XPath:
//button[starts-with(@aria-label,'Search')]
This can be useful when the visible text is unavailable or represented by an icon.
19. starts-with() with data-* Attributes
Applications often use custom data attributes for JavaScript and testing.
<button data-testid="login-button-123">Login</button>
XPath:
//button[starts-with(@data-testid,'login-button-')]
This can be useful when the test ID contains a dynamic suffix.
20. starts-with() with Multiple Conditions
starts-with() can be combined with other XPath conditions using and.
//input[starts-with(@id,'user_') and @type='text']
This selects an input whose ID starts with user_ and whose type is text.
21. starts-with() with OR Condition
The or operator can be used when more than one starting pattern is acceptable.
//input[starts-with(@id,'user_') or starts-with(@id,'username_')]
This XPath matches elements whose ID begins with either prefix.
22. starts-with() with Element Type
Always use the most appropriate element type when possible.
//button[starts-with(@id,'submit_')]
This is generally more specific than:
//*[starts-with(@id,'submit_')]
The second expression searches across all elements and may produce multiple matches.
23. starts-with() with Parent-Child Relationship
starts-with() can be combined with parent-child relationships.
//form//input[starts-with(@id,'user_')]
This searches for input elements inside a form where the ID starts with user_.
24. starts-with() with Descendant Elements
The descendant axis can be used to find matching elements at any level below a parent.
//div[@class='login-form']//input[starts-with(@id,'user_')]
This approach helps narrow the search to a specific section of the page.
25. starts-with() with Parent Selection
You can first identify a child element and then navigate to its parent.
//input[starts-with(@id,'username_')]/parent::div
This selects the parent div of an input whose ID starts with username_.
26. starts-with() with Ancestor
The ancestor axis can be used when the required parent structure is higher in the DOM.
//input[starts-with(@id,'username_')]/ancestor::form
This locates the form containing the matching input.
27. starts-with() with Following-Sibling
starts-with() can be combined with sibling relationships.
//label[starts-with(normalize-space(.),'Username')]/following-sibling::input
This can locate an input that follows a label whose text starts with Username.
28. starts-with() with Table Data
It can be used when table cell text begins with a known value.
//tr[td[starts-with(normalize-space(.),'John')]]
This finds a table row containing a cell whose text starts with John.
29. starts-with() with Dropdown Options
Example:
<option>India - Mumbai</option>
<option>India - Delhi</option>
<option>United States</option>
XPath:
//option[starts-with(normalize-space(.),'India')]
This identifies options whose text starts with India.
30. starts-with() with Search Results
Suppose search results display names beginning with a known prefix:
<div class="result">Apple iPhone</div>
<div class="result">Apple MacBook</div>
XPath:
//div[@class='result' and starts-with(normalize-space(.),'Apple')]
31. starts-with() in Selenium Java
In Selenium Java, the XPath expression is passed to By.xpath().
WebElement username = driver.findElement(By.xpath("//input[starts-with(@id,'username_')]"));
username.sendKeys("admin");
32. Using starts-with() with click()
A dynamic button can be located and clicked using starts-with().
driver.findElement(By.xpath("//button[starts-with(@id,'submit_')]")).click();
33. Using starts-with() with sendKeys()
For dynamic input fields:
driver.findElement(By.xpath("//input[starts-with(@id,'username_')]")).sendKeys("admin");
34. Using starts-with() with getText()
starts-with() can locate an element and Selenium can then retrieve its text.
String text = driver.findElement(By.xpath("//div[starts-with(normalize-space(.),'Welcome')]")).getText();
System.out.println(text);
35. Using starts-with() with getAttribute()
You can retrieve an attribute after locating the element.
WebElement element = driver.findElement(By.xpath("//input[starts-with(@id,'user_')]"));
String id = element.getAttribute("id");
System.out.println(id);
36. Using starts-with() with findElements()
When multiple elements match the same prefix, use findElements().
List<WebElement> elements = driver.findElements(By.xpath("//input[starts-with(@id,'user_')]"));
System.out.println(elements.size());
37. starts-with() with Explicit Wait
Dynamic elements may not be immediately available. Explicit waits can be combined with starts-with() XPath.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[starts-with(@id,'login_')]")));
loginButton.click();
38. starts-with() in Page Object Model
In the Page Object Model, dynamic XPath expressions can be stored in page classes.
By usernameField = By.xpath("//input[starts-with(@id,'username_')]");
By loginButton = By.xpath("//button[starts-with(@id,'login_')]");
public void login(String username) {
driver.findElement(usernameField).sendKeys(username);
driver.findElement(loginButton).click();
}
39. starts-with() for Dynamic Web Applications
Dynamic applications may generate changing IDs, classes, URLs, or other attributes. If a stable prefix exists, starts-with() can help create a locator that does not depend on the changing suffix.
Example:
profile_1001
profile_1002
profile_1003
XPath:
//div[starts-with(@id,'profile_')]
40. Dynamic ID Pattern Example
| Dynamic ID | Common Prefix | XPath |
| user_101 | user_ | //input[starts-with(@id,'user_')] |
| user_202 | user_ | //input[starts-with(@id,'user_')] |
| login_501 | login_ | //button[starts-with(@id,'login_')] |
| order_789 | order_ | //div[starts-with(@id,'order_')] |
41. starts-with() vs Exact Attribute Matching
Exact matching checks the complete attribute value.
//input[@id='username_12345']
starts-with() checks only the beginning.
//input[starts-with(@id,'username_')]
| Method | Purpose |
| @id='value' | Matches the complete attribute value. |
| starts-with(@id,'value') | Matches an attribute beginning with the specified value. |
42. starts-with() vs contains()
starts-with() checks the beginning of a string, while contains() checks whether the specified value occurs anywhere in the string.
//input[starts-with(@id,'user_')]
//input[contains(@id,'user_')]
| Function | Matching Behavior |
| starts-with() | Value must occur at the beginning. |
| contains() | Value can occur anywhere. |
43. starts-with() vs text()
The text() function can be used for exact text matching.
//button[text()='Login']
starts-with() can be used when the beginning is known but the remaining text changes.
//button[starts-with(normalize-space(.),'Login')]
For example, this can match text such as Login Now, Login to Continue, or Login as Admin.
44. starts-with() vs Partial Link Text
Selenium provides By.partialLinkText() for partial matching of link text. XPath starts-with() can provide more flexible conditions involving text and attributes.
driver.findElement(By.partialLinkText("Product")).click();
XPath alternative:
driver.findElement(By.xpath("//a[starts-with(normalize-space(.),'Product')]")).click();
45. starts-with() vs CSS Attribute Prefix Selector
CSS selectors also support prefix matching with ^=.
input[id^='user_']
Equivalent XPath:
//input[starts-with(@id,'user_')]
| XPath | CSS |
| starts-with(@id,'user_') | [id^='user_'] |
46. starts-with() with Multiple Attributes
Multiple starts-with() conditions can be combined.
//input[starts-with(@id,'user_') and starts-with(@name,'username_')]
This is useful when both attributes follow predictable naming patterns.
47. starts-with() with OR Patterns
Sometimes different versions of an application may generate different prefixes.
//input[starts-with(@id,'user_') or starts-with(@id,'usr_')]
This XPath supports both patterns.
48. Case Sensitivity of starts-with()
XPath string comparisons are case-sensitive. For example:
//button[starts-with(normalize-space(.),'Login')]
This does not automatically match text beginning with login or LOGIN.
For case-insensitive matching, XPath can use translate().
//button[starts-with(translate(normalize-space(.),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'login')]
49. Case-Insensitive Attribute Matching
Case-insensitive matching can also be applied to attributes.
//input[starts-with(translate(@id,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'user_')]
This converts uppercase letters to lowercase before performing the comparison.
50. starts-with() with normalize-space()
A robust text-based expression can combine starts-with() and normalize-space().
//div[starts-with(normalize-space(.),'Order')]
This is helpful when the page contains extra whitespace around the displayed text.
51. starts-with() with Nested Span Text
Consider:
<button>
<span>Continue</span>
</button>
A practical XPath is:
//button[starts-with(normalize-space(.),'Continue')]
The dot represents the string value of the button and can include descendant text.
52. starts-with() with Labels
Suppose:
<label>Username Address</label>
XPath:
//label[starts-with(normalize-space(.),'Username')]
This matches the label because its text begins with Username.
53. starts-with() with Form Elements
Example:
//form[starts-with(@id,'registration_')]//input
This identifies input elements inside a form whose ID begins with registration_.
54. starts-with() for Login Automation
A login page may contain dynamic identifiers.
//input[starts-with(@id,'username_')]
//input[starts-with(@id,'password_')]
//button[starts-with(@id,'login_')]
Selenium example:
driver.findElement(By.xpath("//input[starts-with(@id,'username_')]")).sendKeys("admin");
driver.findElement(By.xpath("//input[starts-with(@id,'password_')]")).sendKeys("admin123");
driver.findElement(By.xpath("//button[starts-with(@id,'login_')]")).click();
55. starts-with() for Registration Automation
Dynamic registration fields can be located using stable prefixes.
driver.findElement(By.xpath("//input[starts-with(@id,'firstName_')]")).sendKeys("John");
driver.findElement(By.xpath("//input[starts-with(@id,'lastName_')]")).sendKeys("Smith");
driver.findElement(By.xpath("//input[starts-with(@id,'email_')]")).sendKeys("[email protected]");
56. starts-with() for Search Automation
A search result can be located using text that starts with a known word.
WebElement result = driver.findElement(By.xpath("//div[starts-with(normalize-space(.),'Selenium')]"));
System.out.println(result.getText());
57. starts-with() for Product Automation
Example product elements:
<div id="product_101">Laptop</div>
<div id="product_102">Mobile</div>
XPath:
//div[starts-with(@id,'product_')]
58. starts-with() for Order Automation
Dynamic order IDs can be handled using a stable prefix.
//div[starts-with(@id,'order_')]
For example:
order_1001
order_1002
order_1003
59. starts-with() with Text and Attribute
Multiple conditions can improve locator precision.
//button[starts-with(@id,'submit_') and starts-with(normalize-space(.),'Submit')]
This requires both the ID and visible text to start with the specified values.
60. starts-with() with Class and Text
//button[starts-with(@class,'btn') and starts-with(normalize-space(.),'Login')]
This combines a class prefix with a text prefix.
61. Avoiding Overly Broad starts-with() XPath
A broad XPath such as:
//*[starts-with(@id,'user')]
may match many elements.
A more specific expression is:
//input[starts-with(@id,'user_')]
Using a specific element type and stable prefix generally makes the locator easier to understand and maintain.
62. Checking Multiple Matching Elements
Before using a starts-with() XPath in a test, verify how many elements it matches.
List<WebElement> elements = driver.findElements(By.xpath("//input[starts-with(@id,'user_')]"));
System.out.println("Matches: " + elements.size());
If several elements are returned unexpectedly, refine the XPath.
63. Testing starts-with() in Chrome DevTools
XPath expressions can be tested in Chrome DevTools using the $x() function.
$x("//input[starts-with(@id,'user_')]")
If the XPath is correct, Chrome returns the matching DOM elements.
64. Validating a Text-Based starts-with() XPath
$x("//button[starts-with(normalize-space(.),'Login')]")
This allows you to verify whether the expected button is returned before adding the XPath to Selenium code.
65. starts-with() in Dynamic Tables
Suppose a table contains dynamically generated row IDs:
<tr id="row_1001">...</tr>
<tr id="row_1002">...</tr>
XPath:
//tr[starts-with(@id,'row_')]
66. Selecting a Specific Row with starts-with()
starts-with() can be combined with text conditions.
//tr[starts-with(@id,'row_') and td[starts-with(normalize-space(.),'John')]]
This selects a row whose ID starts with row_ and contains a table cell beginning with John.
67. starts-with() with Index
An index can be applied when multiple elements match.
(//input[starts-with(@id,'user_')])[1]
This selects the first matching input.
Indexing should be used carefully because the order of elements can change.
68. starts-with() with Last Matching Element
(//input[starts-with(@id,'user_')])[last()]
This selects the last matching input element.
69. starts-with() with Position
(//button[starts-with(@id,'submit_')])[2]
This selects the second matching button.
70. starts-with() in Page Object Model Example
public class LoginPage {
private WebDriver driver;
private By username = By.xpath("//input[starts-with(@id,'username_')]");
private By password = By.xpath("//input[starts-with(@id,'password_')]");
private By loginButton = By.xpath("//button[starts-with(@id,'login_')]");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(String user, String pass) {
driver.findElement(username).sendKeys(user);
driver.findElement(password).sendKeys(pass);
driver.findElement(loginButton).click();
}
}
71. starts-with() with Explicit Wait in POM
private By loginButton = By.xpath("//button[starts-with(@id,'login_')]");
public void clickLogin() {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(loginButton));
button.click();
}
72. Real-World Automation Flow
- Open the application.
- Inspect the required element.
- Identify the dynamic attribute.
- Find the stable beginning of the attribute.
- Create a starts-with() XPath.
- Validate the XPath in DevTools.
- Check whether the XPath returns the expected number of elements.
- Use the XPath with Selenium WebDriver.
- Perform the required action such as click(), sendKeys(), or getText().
- Use explicit waits when the element is dynamically loaded.
73. Common Mistake: Using Complete Dynamic ID
Suppose the ID changes from:
username_12345
to:
username_67890
This XPath is fragile:
//input[@id='username_12345']
A prefix-based XPath is more appropriate when the prefix is stable:
//input[starts-with(@id,'username_')]
74. Common Mistake: Incorrect Prefix
If the actual value is:
user_123
This will not match:
//input[starts-with(@id,'username_')]
The correct prefix is:
//input[starts-with(@id,'user_')]
75. Common Mistake: Case Mismatch
XPath matching is case-sensitive.
For:
<button>Login Now</button>
This matches:
//button[starts-with(normalize-space(.),'Login')]
But this does not match the same text:
//button[starts-with(normalize-space(.),'login')]
76. Common Mistake: Using starts-with() When Exact Match Is Better
If the attribute value is stable and unique, exact matching can be clearer.
//input[@id='username']
There is no need to use:
//input[starts-with(@id,'user')]
unless partial matching is actually required.
77. Common Mistake: Broad Text Matching
This expression may match several elements:
//*[starts-with(normalize-space(.),'Login')]
A more specific locator is:
//button[starts-with(normalize-space(.),'Login')]
78. Common Mistake: Ignoring Multiple Matches
starts-with() may return multiple elements when the same prefix is used repeatedly.
List<WebElement> matches = driver.findElements(By.xpath("//input[starts-with(@id,'user_')]"));
System.out.println(matches.size());
Always verify uniqueness before relying on a locator.
79. Common Mistake: Overusing Indexes
This expression depends on element position:
(//button[starts-with(@id,'login_')])[1]
If the page structure changes, the first matching button may change. Prefer a more specific relationship or stable attribute whenever possible.
80. Best Practices for starts-with()
- Use starts-with() when the beginning of an attribute or text is stable.
- Prefer stable attributes over generated values.
- Use a specific element type when possible.
- Combine starts-with() with other conditions when necessary.
- Use normalize-space() for text matching when whitespace may vary.
- Validate XPath expressions in browser DevTools.
- Check the number of matching elements before using the locator.
- Avoid unnecessarily broad expressions.
- Avoid indexes unless there is no better stable relationship.
- Use explicit waits for dynamically loaded elements.
- Keep XPath expressions readable in Page Object classes.
81. Advantages of starts-with()
- Useful for dynamic attribute values.
- Can avoid hardcoding changing suffixes.
- Works with attributes and text.
- Can be combined with AND and OR conditions.
- Can be combined with XPath axes.
- Useful for dynamic IDs and URLs.
- Useful in real-world Selenium automation.
- Can make locators more flexible when a stable prefix exists.
82. Limitations of starts-with()
- It only checks the beginning of a string.
- It is case-sensitive unless additional XPath functions are used.
- It can return multiple elements if the prefix is not unique.
- It should not be used when an exact stable locator is already available.
- It can become fragile if the prefix itself changes.
- Broad text-based expressions can match unexpected elements.
83. When Should You Use starts-with()?
- When an ID has a stable prefix and dynamic suffix.
- When a name attribute starts with a predictable value.
- When a URL starts with a known path.
- When visible text begins with a known phrase.
- When generated attributes contain stable prefixes.
- When working with dynamic web applications.
- When an exact attribute value is not stable.
84. When Should You Avoid starts-with()?
- When a stable unique ID is available.
- When the complete attribute value is static.
- When the prefix is not unique.
- When the beginning of the value changes frequently.
- When a more stable parent-child relationship is available.
- When a simple CSS selector provides a clearer locator.
85. Practical Exercise
Consider the following HTML:
<div class="login-container">
<input id="user_12345" type="text" placeholder="Enter username">
<input id="pass_98765" type="password" placeholder="Enter password">
<button id="login_56789">Login to Account</button>
</div>
Create XPath expressions using starts-with().
Username:
//input[starts-with(@id,'user_')]
Password:
//input[starts-with(@id,'pass_')]
Login button:
//button[starts-with(@id,'login_')]
Login button using text:
//button[starts-with(normalize-space(.),'Login')]
86. Practical Selenium Exercise
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement username = driver.findElement(By.xpath("//input[starts-with(@id,'user_')]"));
username.sendKeys("admin");
WebElement password = driver.findElement(By.xpath("//input[starts-with(@id,'pass_')]"));
password.sendKeys("admin123");
WebElement login = driver.findElement(By.xpath("//button[starts-with(@id,'login_')]"));
login.click();
87. Real-World Example: Dynamic E-Commerce Application
Suppose an e-commerce application generates product IDs like:
product_1001
product_1002
product_1003
XPath:
//div[starts-with(@id,'product_')]
If the product name also begins with a known value, additional conditions can be used:
//div[starts-with(@id,'product_') and starts-with(normalize-space(.),'Laptop')]
88. Real-World Example: Dynamic Order Page
Suppose order containers use IDs such as:
order_10001
order_10002
order_10003
XPath:
//div[starts-with(@id,'order_')]
Specific order text can be combined with the dynamic prefix.
//div[starts-with(@id,'order_') and starts-with(normalize-space(.),'Order')]
89. Quick Reference
| Requirement | XPath |
| Dynamic ID | //input[starts-with(@id,'user_')] |
| Dynamic Name | //input[starts-with(@name,'user_')] |
| Dynamic Class | //button[starts-with(@class,'btn-')] |
| Dynamic Href | //a[starts-with(@href,'/products/')] |
| Text Prefix | //button[starts-with(normalize-space(.),'Login')] |
| Placeholder Prefix | //input[starts-with(@placeholder,'Enter')] |
| ARIA Label Prefix | //button[starts-with(@aria-label,'Search')] |
| Data Attribute Prefix | //button[starts-with(@data-testid,'login-')] |
| Multiple Conditions | //input[starts-with(@id,'user_') and @type='text'] |
90. Important XPath Patterns
//input[starts-with(@id,'user_')]
//button[starts-with(@id,'login_')]
//a[starts-with(@href,'/products/')]
//input[starts-with(@name,'email_')]
//button[starts-with(normalize-space(.),'Login')]
//div[starts-with(normalize-space(.),'Order')]
//input[starts-with(@placeholder,'Enter')]
//button[starts-with(@aria-label,'Search')]
//tr[starts-with(@id,'row_')]
//option[starts-with(normalize-space(.),'India')]
91. Interview Questions
Q1. What is starts-with() in XPath?
starts-with() is an XPath string function used to check whether a string begins with a specified value. In Selenium, it is commonly used to locate elements with dynamic attributes or text.
Q2. What is the syntax of starts-with()?
starts-with(string, substring)
Q3. How do you locate an element with a dynamic ID?
//input[starts-with(@id,'user_')]
Q4. Can starts-with() be used with text?
Yes. For example:
//button[starts-with(normalize-space(.),'Login')]
Q5. What is the difference between starts-with() and contains()?
starts-with() checks the beginning of a string, whereas contains() checks whether a value occurs anywhere in the string.
Q6. Is starts-with() case-sensitive?
Yes. XPath string matching is case-sensitive unless functions such as translate() are used to normalize the case.
Q7. Can starts-with() be combined with AND?
Yes.
//input[starts-with(@id,'user_') and @type='text']
Q8. Can starts-with() be used with attributes other than ID?
Yes. It can be applied to attributes such as name, class, href, placeholder, aria-label, data-testid, and other attributes.
Q9. Can starts-with() be used with normalize-space()?
Yes. A common text-based expression is:
//button[starts-with(normalize-space(.),'Login')]
Q10. How can you test starts-with() XPath in Chrome?
Use the browser console:
$x("//input[starts-with(@id,'user_')]")
92. Interview Scenario
Question: A username field has IDs such as username_123, username_456, and username_789. How would you locate it?
Answer:
//input[starts-with(@id,'username_')]
If multiple elements match, add another stable condition such as the element type, placeholder, form relationship, or another reliable attribute.
93. Learning Checklist
- Understand what starts-with() means.
- Learn the starts-with() syntax.
- Practice starts-with() with ID.
- Practice starts-with() with name.
- Practice starts-with() with class.
- Practice starts-with() with href.
- Practice starts-with() with placeholder.
- Practice starts-with() with text.
- Understand normalize-space().
- Understand the difference between text() and dot.
- Combine starts-with() with AND.
- Combine starts-with() with OR.
- Use starts-with() with XPath axes.
- Test XPath using Chrome DevTools.
- Use starts-with() with Selenium Java.
- Use starts-with() with explicit waits.
- Apply starts-with() in Page Object Model.
- Understand its limitations.
- Avoid unnecessary indexes.
- Verify locator uniqueness.
94. Key Points to Remember
- starts-with() checks whether a string begins with a specified value.
- It is especially useful for dynamic web elements.
- It can be used with attributes and text.
- It is useful when the beginning of an attribute remains stable.
- It is case-sensitive.
- normalize-space() can improve text-based matching.
- The dot operator can help when text is contained inside child elements.
- AND and OR can be used to create more precise conditions.
- Always verify whether the XPath returns a unique element.
- Prefer stable and meaningful locator strategies.
95. Selenium Training Resource
To continue learning Selenium WebDriver, locators, WebElements, TestNG, Page Object Model, automation frameworks, and real-world automation projects, explore the JustAcademy Selenium Training Course.
You can also explore the Selenium Training Demo Registration for the course.
96. Conclusion
The starts-with() XPath function is an important technique for Selenium automation when web elements contain dynamic values but have a predictable beginning. It is particularly useful for dynamic IDs, names, URLs, placeholders, accessibility attributes, data attributes, and text patterns.
A strong Selenium automation tester should understand when to use starts-with(), when to use contains(), when exact attribute matching is better, and how to combine XPath functions with relationships and other conditions. With proper validation and stable locator design, starts-with() can be a useful part of maintainable Selenium automation scripts.
For structured Selenium learning covering WebDriver, locators, TestNG, POM, automation frameworks, cross-browser testing, and real-time projects, visit Selenium Training or register for the Selenium Course Demo.