0
0
Selenium Javatesting~10 mins

Why selector mastery prevents fragile tests 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 find an element by its unique ID using Selenium in Java.

Selenium Java
WebElement element = driver.findElement(By.[1]("submit-button"));
Drag options to blanks, or click blank then click option'
AlinkText
BclassName
CtagName
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using By.className when the ID is available
Using By.tagName which is too broad
Using By.linkText for non-link elements
2fill in blank
medium

Complete the code to locate an element using a CSS selector that targets a button with class 'btn-primary'.

Selenium Java
WebElement button = driver.findElement(By.[1]("button.btn-primary"));
Drag options to blanks, or click blank then click option'
Axpath
BcssSelector
Cid
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using By.id when no ID exists
Using By.name which may not be unique
Using By.xpath unnecessarily for simple selectors
3fill in blank
hard

Fix the error in the XPath selector to find a link with text 'Home'.

Selenium Java
WebElement homeLink = driver.findElement(By.xpath("//a[[1]='Home']"));
Drag options to blanks, or click blank then click option'
Avalue
BinnerText
Ctext()
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using innerText or value instead of text()
Using name attribute which may not exist
Forgetting to use quotes around 'Home'
4fill in blank
hard

Fill both blanks to create a CSS selector that finds an input element with type 'checkbox' and name 'subscribe'.

Selenium Java
WebElement checkbox = driver.findElement(By.cssSelector("input[1][[2]='subscribe']"));
Drag options to blanks, or click blank then click option'
A[type='checkbox']
Bname
Ctype
D.checkbox
Attempts:
3 left
💡 Hint
Common Mistakes
Using .checkbox which selects by class, not attribute
Swapping 'type' and 'name' in the selector
Omitting the attribute selector syntax
5fill in blank
hard

Fill all three blanks to create a Java Selenium code snippet that finds all visible buttons with class 'action' and asserts the count is 3.

Selenium Java
List<WebElement> buttons = driver.findElements(By.[1]("button.action"));
int visibleCount = 0;
for (WebElement btn : buttons) {
    if (btn.is[2]()) {
        visibleCount[3];
    }
}
assertEquals(3, visibleCount);
Drag options to blanks, or click blank then click option'
AcssSelector
BDisplayed
C++
Dxpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using xpath unnecessarily for simple selectors
Using isEnabled() instead of isDisplayed() for visibility
Forgetting to increment the counter