0
0
Selenium Javatesting~20 mins

findElement by cssSelector in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSS Selector Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
locator
intermediate
2:00remaining
Identify the correct CSS selector to find a button with class 'submit-btn' inside a form with id 'loginForm'
Which CSS selector will correctly locate the submit button inside the form with id 'loginForm'?
Aform#loginForm > button.submit-btn
B.submit-btn #loginForm
C#loginForm .submit-btn
Dbutton#loginForm.submit-btn
Attempts:
2 left
💡 Hint
Remember that the selector should find an element with class inside the form with the given id.
Predict Output
intermediate
2:00remaining
What happens in this Selenium Java code snippet?
Given the following code, what will happen if no such element exists?
Selenium Java
WebElement element = driver.findElement(By.cssSelector("div.content > p.highlight"));
System.out.println(element.getText());
APrints the text inside the first <p> with class 'highlight' inside a <div> with class 'content'
BThrows NoSuchElementException if no such element exists
CPrints the HTML of the <p> element
DPrints the tag name 'p'
Attempts:
2 left
💡 Hint
Consider what happens if the element is not found by findElement.
assertion
advanced
2:00remaining
Which assertion correctly verifies the presence of an element found by CSS selector?
You want to assert that an element with CSS selector '.nav-item.active' is displayed on the page. Which assertion is correct?
Selenium Java
WebElement element = driver.findElement(By.cssSelector(".nav-item.active"));
AassertFalse(element.isEnabled());
BassertEquals(element.getText(), "true");
CassertNotNull(driver.findElement(By.cssSelector(".nav-item.active")));
DassertTrue(element.isDisplayed());
Attempts:
2 left
💡 Hint
Check which method confirms the element is visible on the page.
🔧 Debug
advanced
2:00remaining
Why does this CSS selector cause NoSuchElementException?
Given the HTML structure:
<ul id="menu">
  <li class="item">Home</li>
  <div>
    <li class="item active">About</li>
  </div>
</ul>

The code below throws NoSuchElementException. Why?
driver.findElement(By.cssSelector("ul#menu > li.active"));
ABecause the selector should be "ul#menu li.active" without the > combinator
BBecause the selector is correct but the element is not visible
CBecause the id selector #menu is invalid
DBecause the selector looks for a direct child <li> with class 'active', but the <li> has classes 'item active' and the space causes mismatch
Attempts:
2 left
💡 Hint
Check the relationship between
    and
  • elements and the meaning of > in CSS selectors.
framework
expert
2:00remaining
How to implement a reusable method to find elements by CSS selector with timeout in Selenium Java?
You want to create a method that waits up to a given timeout for an element to be present using a CSS selector. Which implementation is correct?
A
public WebElement waitForElement(By selector, int timeoutSeconds) {
  WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds));
  return wait.until(ExpectedConditions.presenceOfElementLocated(selector));
}
B
public WebElement waitForElement(String cssSelector, int timeoutSeconds) {
  WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds));
  return wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(cssSelector)));
}
C
public WebElement waitForElement(String cssSelector, int timeoutSeconds) {
  WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
  return wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(cssSelector)));
}
D
public WebElement waitForElement(By selector, int timeoutSeconds) {
  WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutSeconds));
  return wait.until(ExpectedConditions.visibilityOfElementLocated(selector));
}
Attempts:
2 left
💡 Hint
Check the correct constructor usage of WebDriverWait and the expected condition for presence.