Recall & Review
beginner
What does
findElement(By.cssSelector()) do in Selenium?It locates the first web element on the page that matches the given CSS selector string.
Click to reveal answer
beginner
Write a CSS selector to find an element with class
btn-primary.Use <code>".btn-primary"</code> as the CSS selector. The dot <code>.</code> means class in CSS selectors.Click to reveal answer
beginner
How do you find an element with id
login using CSS selector?Use
"#login" as the CSS selector. The hash # means id in CSS selectors.Click to reveal answer
intermediate
What is the difference between
findElement and findElements?findElement returns the first matching element or throws an exception if none found. findElements returns a list of all matching elements or an empty list if none found.Click to reveal answer
intermediate
Why is using CSS selectors often preferred over XPath in Selenium?
CSS selectors are usually faster and simpler to write. They are well supported by browsers and easier to read for many common cases.
Click to reveal answer
Which CSS selector finds all elements with class 'active'?
✗ Incorrect
The dot (.) before 'active' selects elements by class name.
What does the CSS selector '#submitBtn' select?
✗ Incorrect
The hash (#) selects elements by their id attribute.
What happens if
findElement(By.cssSelector()) finds no matching element?✗ Incorrect
findElement throws NoSuchElementException if no element matches.
Which CSS selector finds all
input elements inside a form?✗ Incorrect
"form input" selects all input elements inside any form, at any depth.
Which is a valid CSS selector to find a button with both classes 'btn' and 'primary'?
✗ Incorrect
Multiple classes are chained with dots: .btn.primary means element with both classes.
Explain how to use
findElement(By.cssSelector()) to locate a web element by class and id.Think about how CSS selectors identify elements by id and class.
You got /4 concepts.
Describe the difference between
findElement and findElements when using CSS selectors in Selenium.Consider what happens if no elements match.
You got /4 concepts.