0
0
Selenium Javatesting~10 mins

Why reliable element location ensures stability in Selenium Java - Test Your Understanding

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

Complete the code to locate a button by its ID using Selenium in Java.

Selenium Java
WebElement button = driver.findElement(By.[1]("submitBtn"));
Drag options to blanks, or click blank then click option'
AtagName
Bname
CclassName
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using className or tagName can select multiple elements, causing unstable tests.
Using name may not be unique, leading to flaky tests.
2fill in blank
medium

Complete the code to wait until the element located by CSS selector is visible.

Selenium Java
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.[1](".login-form")));
Drag options to blanks, or click blank then click option'
AcssSelector
Bid
Cxpath
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'id' when the selector is a class selector (starts with a dot).
Using 'name' which does not match CSS selector syntax.
3fill in blank
hard

Fix the error in the locator to find an element by XPath.

Selenium Java
WebElement element = driver.findElement(By.[1]("//div[@class='content']"));
Drag options to blanks, or click blank then click option'
AcssSelector
Bid
Cxpath
DtagName
Attempts:
3 left
💡 Hint
Common Mistakes
Using cssSelector with an XPath string causes errors.
Using id or tagName with XPath string is invalid.
4fill in blank
hard

Fill both blanks to create a stable locator that finds a button by its text using XPath.

Selenium Java
WebElement button = driver.findElement(By.[1]("//button[[2]()='Submit']"));
Drag options to blanks, or click blank then click option'
Axpath
BcssSelector
Ctext()
DclassName
Attempts:
3 left
💡 Hint
Common Mistakes
Using cssSelector instead of xpath for text matching.
Using className which cannot match element text.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps element IDs to their text if the text length is greater than 3.

Selenium Java
Map<String, String> elementsText = elements.stream()
  .filter(e -> e.getText().length() [1] 3)
  .collect(Collectors.toMap(e -> e.getAttribute([2]), e -> e.[3]()));
Drag options to blanks, or click blank then click option'
A>
B"id"
CgetText
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the filter condition.
Using wrong attribute name instead of 'id'.
Using wrong method instead of getText() to get element text.