0
0
Selenium Javatesting~10 mins

XPath functions (contains, starts-with) 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 find an element whose text contains 'Submit'.

Selenium Java
WebElement button = driver.findElement(By.xpath("//button[[1](text(), 'Submit')]");
Drag options to blanks, or click blank then click option'
Astarts-with(text(),
Btext()=
Ccontains(text(),
Dends-with(text(),
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'starts-with' instead of 'contains' when searching for substring anywhere.
Using 'text()=' which looks for exact match.
2fill in blank
medium

Complete the code to find an element whose id starts with 'user_'.

Selenium Java
WebElement input = driver.findElement(By.xpath("//input[[1](@id, 'user_')]");
Drag options to blanks, or click blank then click option'
Astarts-with
Bcontains
Cends-with
Dequals
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contains' which matches anywhere, not just start.
Using 'ends-with' which is not supported in XPath 1.0.
3fill in blank
hard

Fix the error in the XPath expression to find a link whose href contains 'login'.

Selenium Java
WebElement link = driver.findElement(By.xpath("//a[[1](@href, 'login')]");
Drag options to blanks, or click blank then click option'
Acontains
Bstart-with
Cstarts-with
Dcontain
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling 'starts-with' as 'start-with'.
Using 'contain' which is not a valid XPath function.
4fill in blank
hard

Fill both blanks to find a div element whose class starts with 'alert' and contains 'error'.

Selenium Java
WebElement alertDiv = driver.findElement(By.xpath("//div[[1](@class, 'alert') and [2](@class, 'error')]");
Drag options to blanks, or click blank then click option'
Astarts-with
Bequals
Ccontains
Dends-with
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'equals' which requires exact match.
Using 'ends-with' which is not supported in XPath 1.0.
5fill in blank
hard

Fill all three blanks to create a map of elements with keys as uppercase tag names and values as text, filtering only those whose text starts with 'Test'.

Selenium Java
Map<String, String> elementsMap = elements.stream()
  .filter(e -> e.getText().[1]("Test"))
  .collect(Collectors.toMap(e -> e.getTagName().[2](), e -> e.[3]()));
Drag options to blanks, or click blank then click option'
AstartsWith
BtoUpperCase
CgetText
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contains' instead of 'startsWith' for filtering.
Using 'getText' incorrectly on the key instead of value.