0
0
Selenium Javatesting~10 mins

XPath with text() in Selenium Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to locate a button with exact text 'Submit'.

Selenium Java
WebElement button = driver.findElement(By.xpath("//button[text()=[1]]"));
Drag options to blanks, or click blank then click option'
A'Submit'
B"Submit"
CSubmit
D"'Submit'"
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes inside double quotes without escaping causes syntax errors.
Not quoting the text value causes XPath to fail.
2fill in blank
medium

Complete the XPath to find a link (<a>) element containing the text 'Learn More'.

Selenium Java
WebElement link = driver.findElement(By.xpath("//a[contains(text(), [1])]"));
Drag options to blanks, or click blank then click option'
A'Learn More'
BLearn More
C"'Learn More'"
D"Learn More"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted text inside contains() causes XPath errors.
Using double quotes inside double quotes without escaping causes syntax errors.
3fill in blank
hard

Fix the error in the XPath to select a div with text exactly 'Welcome'.

Selenium Java
WebElement div = driver.findElement(By.xpath("//div[text()=[1]]"));
Drag options to blanks, or click blank then click option'
A"'Welcome'"
B'Welcome'
CWelcome
D"Welcome"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted text causes XPath syntax errors.
Using double quotes inside double quotes without escaping causes Java string errors.
4fill in blank
hard

Fill both blanks to locate a span element containing text 'Error' and having class 'alert'.

Selenium Java
WebElement alertSpan = driver.findElement(By.xpath("//span[contains(text(), [1]) and contains(@class, [2])]"));
Drag options to blanks, or click blank then click option'
A'Error'
B"Error"
C'alert'
D"alert"
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes inside double quotes without escaping causes syntax errors.
Not quoting the attribute or text value causes XPath to fail.
5fill in blank
hard

Fill all three blanks to find a button with exact text 'Cancel', visible, and with id 'btnCancel'.

Selenium Java
WebElement cancelBtn = driver.findElement(By.xpath("//button[text()=[1] and @id=[2] and not(contains(@style, [3]))]"));
Drag options to blanks, or click blank then click option'
A'Cancel'
B'btnCancel'
C'display:none'
D"Cancel"
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes inside double quotes without escaping.
Not quoting string literals properly.
Missing the visibility check causes hidden elements to be selected.