Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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
✗ Incorrect
Using By.id is the most reliable way to locate an element with a unique ID, which helps prevent fragile tests.
2fill in blank
mediumComplete 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'
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
✗ Incorrect
Using By.cssSelector with 'button.btn-primary' targets a button element with the class 'btn-primary', which is precise and less fragile.
3fill in blank
hardFix 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'
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'
✗ Incorrect
XPath uses the function text() to match the visible text of an element. Using 'text()' correctly locates the link with text 'Home'.
4fill in blank
hardFill 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'
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
✗ Incorrect
The selector input[type='checkbox'][name='subscribe'] correctly targets the checkbox input with the specified name attribute.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using xpath unnecessarily for simple selectors
Using isEnabled() instead of isDisplayed() for visibility
Forgetting to increment the counter
✗ Incorrect
Using By.cssSelector to find buttons with class 'action', checking visibility with isDisplayed(), and incrementing visibleCount with ++ correctly counts visible buttons.