0
0
Selenium Javatesting~20 mins

XPath with attributes in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XPath Attribute Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
locator
intermediate
2:00remaining
Locate element by single attribute
Which XPath expression correctly locates a button element with the attribute id='submitBtn'?
A//button[id='submitBtn']
B//button[@id='submitBtn']
C//button[@id:= 'submitBtn']
D//button[@id='submitBtn']/
Attempts:
2 left
💡 Hint
Remember that attribute selectors in XPath use @ before the attribute name and use = for comparison.
assertion
intermediate
2:00remaining
Verify element text using XPath with attribute
Given the XPath //div[@class='alert'], which assertion correctly verifies the element's text equals 'Success' in Java Selenium?
Selenium Java
WebElement alert = driver.findElement(By.xpath("//div[@class='alert']"));
AassertEquals(alert.getText(), "Success");
BassertTrue(alert.getText() == "Success");
CassertEquals("Success", alert.getText());
DassertTrue(alert.getText().equalsIgnoreCase("Success"));
Attempts:
2 left
💡 Hint
Use assertEquals(expected, actual) for exact string match.
Predict Output
advanced
2:00remaining
Output of XPath with multiple attributes
What is the output of the following Java Selenium code snippet if the page contains exactly one <input type='text' name='username' value='user1'> element?
Selenium Java
WebElement input = driver.findElement(By.xpath("//input[@type='text' and @name='username']"));
System.out.println(input.getAttribute("value"));
Auser1
Bnull
Cusername
DSyntaxError
Attempts:
2 left
💡 Hint
The XPath selects input with type text and name username, then prints its value attribute.
🔧 Debug
advanced
2:00remaining
Identify the error in XPath with attribute contains
What error will the following XPath expression cause in Selenium if used as By.xpath("//a[contains(@href, 'login')"]?
ASyntaxError due to missing closing parenthesis
BNoSuchElementException because no element matches
CInvalidSelectorException due to unbalanced quotes
DNo error, works correctly
Attempts:
2 left
💡 Hint
Check if all parentheses and quotes are properly closed.
framework
expert
3:00remaining
Best practice for locating dynamic elements with attributes
In a Selenium Java test framework, which approach is best to locate a button with a dynamic id attribute that always starts with btn_ and has a fixed data-action='submit' attribute?
AUse XPath: //button[contains(@id, 'btn_') and @data-action='submit']
BUse CSS selector: button[id*='btn_'][data-action='submit']
CUse CSS selector: button[id^='btn_'][data-action='submit']
DUse XPath: //button[starts-with(@id, 'btn_') and @data-action='submit']
Attempts:
2 left
💡 Hint
Consider which selector matches the start of the id attribute exactly and the fixed attribute.