XPath with attributes helps you find specific elements on a web page by looking at their properties. This makes your tests more accurate and reliable.
XPath with attributes in Selenium Java
//tagName[@attributeName='attributeValue']Use double slashes // to search anywhere in the page.
Attributes are inside square brackets with @ before the attribute name.
//input[@id='username']
//button[@class='submit-btn']//a[@href='https://example.com']//div[@data-test='login-form']This test opens a login page, finds elements using XPath with attributes, enters credentials, clicks login, and checks if login succeeded by page title.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class XPathWithAttributesTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/login"); // Find username input by id attribute WebElement usernameInput = driver.findElement(By.xpath("//input[@id='username']")); usernameInput.sendKeys("testuser"); // Find password input by name attribute WebElement passwordInput = driver.findElement(By.xpath("//input[@name='password']")); passwordInput.sendKeys("password123"); // Find login button by class attribute and click WebElement loginButton = driver.findElement(By.xpath("//button[@class='login-btn']")); loginButton.click(); // Simple check: verify page title contains 'Dashboard' String title = driver.getTitle(); if (title.contains("Dashboard")) { System.out.println("Test Passed: Logged in successfully."); } else { System.out.println("Test Failed: Login unsuccessful."); } } finally { driver.quit(); } } }
Always use unique attributes to avoid selecting wrong elements.
Use browser DevTools to test XPath expressions before using them in code.
Remember to handle exceptions if elements are not found to avoid test crashes.
XPath with attributes helps find elements by their properties.
Use syntax like //tag[@attr='value'] for precise selection.
Testing XPath in browser tools saves time and errors.
Practice
//input[@type='submit'] select in a web page?Solution
Step 1: Understand XPath tag and attribute syntax
The expression uses//inputto select all <input> tags anywhere in the document.Step 2: Analyze the attribute filter
The filter[@type='submit']selects only those <input> elements whose 'type' attribute exactly equals 'submit'.Final Answer:
All <input> elements with attribute type equal to 'submit' -> Option AQuick Check:
XPath //tag[@attr='value'] selects elements with that attribute value [OK]
- Confusing tag name with attribute name
- Using wrong attribute syntax like @type=submit without quotes
- Thinking it selects elements named 'submit'
Solution
Step 1: Recall correct attribute syntax in XPath
Attributes are selected using@attributeName='value'inside square brackets.Step 2: Check each option
//button[@id='loginBtn'] uses@id='loginBtn'correctly. Options A, B, and D use invalid syntax for attributes.Final Answer:
//button[@id='loginBtn'] -> Option DQuick Check:
Attributes need '@' before name in XPath [OK]
- Omitting '@' before attribute name
- Using parentheses or '#' incorrectly
- Confusing XPath with CSS selector syntax
<input type='checkbox' name='subscribe' checked>
What will the XPath
//input[@checked] select?Solution
Step 1: Understand attribute presence in XPath
The XPath//input[@checked]selects all <input> elements that have the 'checked' attribute, regardless of its value.Step 2: Analyze the HTML snippet
The <input> element has the 'checked' attribute present (without a value), so it matches the XPath.Final Answer:
All <input> elements with attribute 'checked' present -> Option AQuick Check:
XPath [@attr] checks attribute presence, not value [OK]
- Assuming attribute must have a value
- Confusing attribute presence with value equality
- Thinking empty attributes are ignored
//div[@class='container'] but it does not find the element. The element's class attribute is "container main". What is the issue?Solution
Step 1: Understand attribute matching in XPath
Using@class='container'matches only if the class attribute is exactly 'container'.Step 2: Analyze the actual class attribute
The element's class is 'container main', which is not an exact match, so XPath does not select it.Final Answer:
XPath looks for exact match; 'container main' ≠ 'container' -> Option BQuick Check:
Attribute equality in XPath is exact, no partial match [OK]
- Assuming XPath matches partial attribute values
- Using wrong attribute name like 'className'
- Thinking quotes style causes failure
<input> elements that have a type attribute equal to 'text' or 'password' using XPath. Which expression is correct?Solution
Step 1: Understand logical operators in XPath
To select elements matching either condition, useorbetween attribute tests.Step 2: Analyze each option
//input[@type='text' or @type='password'] correctly usesorto select inputs with type 'text' or 'password'. //input[@type='text' and @type='password'] usesandwhich is impossible for one attribute to have two values simultaneously. Options C and D use invalid syntax.Final Answer:
//input[@type='text' or @type='password'] -> Option CQuick Check:
Use 'or' to combine attribute conditions in XPath [OK]
- Using 'and' instead of 'or' for multiple attribute values
- Trying to list values inside one attribute test
- Incorrect syntax with parentheses or commas
Ready to prove it?
Pass the test and this pattern moves unknown
