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'?
A"#active"
B"active"
C".active"
D"*active"
✗ Incorrect
The dot (.) before 'active' selects elements by class name.
What does the CSS selector '#submitBtn' select?
AElements with id 'submitBtn'
BElements with name 'submitBtn'
CAll buttons
DElements with class 'submitBtn'
✗ Incorrect
The hash (#) selects elements by their id attribute.
What happens if findElement(By.cssSelector()) finds no matching element?
AThrows NoSuchElementException
BReturns an empty list
CReturns null
DReturns a default element
✗ Incorrect
findElement throws NoSuchElementException if no element matches.
Which CSS selector finds all input elements inside a form?
A"form > input"
B"form input"
C"input form"
D"form + input"
✗ 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'?
A".btn#primary"
B"#btn.primary"
C"btn.primary"
D".btn.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.
Practice
(1/5)
1. What does driver.findElement(By.cssSelector(".btn-primary")) do in Selenium Java?
easy
A. Finds all elements with class 'btn-primary' on the page
B. Finds the first element with class 'btn-primary' on the page
C. Finds the element with id 'btn-primary'
D. Throws an error if any element has class 'btn-primary'
Solution
Step 1: Understand the method used
findElement returns the first matching element found on the page.
Step 2: Analyze the CSS selector
The selector .btn-primary matches elements with class btn-primary.
Final Answer:
Finds the first element with class 'btn-primary' on the page -> Option B
Quick Check:
findElement + .class = first matching element [OK]
Hint: findElement + .class finds first element with that class [OK]
Common Mistakes:
Confusing findElement with findElements
Using id selector syntax for class
Thinking it finds all matching elements
2. Which of the following is the correct syntax to find an element with id 'login' using CSS selector in Selenium Java?
easy
A. driver.findElement(By.cssSelector(".login"));
B. driver.findElement(By.cssSelector("*login"));
C. driver.findElement(By.cssSelector("login"));
D. driver.findElement(By.cssSelector("#login"));
Solution
Step 1: Identify CSS selector for id
In CSS, id selectors start with #, so #login selects element with id 'login'.
Step 2: Check syntax correctness
The syntax driver.findElement(By.cssSelector("#login")); is correct for Selenium Java.
Final Answer:
driver.findElement(By.cssSelector("#login")); -> Option D
Quick Check:
Id selector uses # [OK]
Hint: Use # for id selectors in CSS [OK]
Common Mistakes:
Using . instead of # for id
Missing quotes around selector
Using invalid selector syntax
3. What will be the result of this code snippet?
WebElement element = driver.findElement(By.cssSelector("div.content > p.highlight")); String text = element.getText(); System.out.println(text);
A. CSS selector requires both id and class but element has no class 'active'
B. Syntax error in findElement method call
C. button.click() is invalid method
D. NoSuchElementException because id selector is wrong
Solution
Step 1: Analyze the CSS selector
The selector button#submit-btn.active looks for a button with id submit-btn AND class active.
Step 2: Compare with HTML element
The button has id submit-btn but no class active, so selector matches no element.
Final Answer:
CSS selector requires both id and class but element has no class 'active' -> Option A
Quick Check:
Selector requires all parts to match [OK]
Hint: All parts in CSS selector must match element attributes [OK]
Common Mistakes:
Assuming partial match works in CSS selector
Ignoring missing class in element
Thinking click() method is invalid
5. You want to find the first input element inside a form with class 'search' that is enabled and visible. Which CSS selector is best to use with findElement in Selenium Java?
hard
A. form.search input:not([disabled])
B. form.search > input[disabled='false']
C. form.search input:enabled
D. form.search input[visible='true']
Solution
Step 1: Understand CSS selectors for enabled inputs
CSS does not have a standard :enabled pseudo-class for all browsers; instead, :not([disabled]) selects inputs without the disabled attribute.
Step 2: Check options for correctness
form.search input:enabled uses :enabled which is not widely supported in CSS selectors for Selenium. form.search > input[disabled='false'] uses attribute [disabled='false'] which is invalid because disabled is boolean attribute. form.search input:not([disabled]) uses :not([disabled]) which correctly selects enabled inputs. form.search input[visible='true'] uses [visible='true'] which is not a valid attribute for visibility.
Final Answer:
form.search input:not([disabled]) -> Option A
Quick Check:
Use :not([disabled]) to find enabled inputs [OK]
Hint: Use :not([disabled]) to select enabled inputs [OK]
Common Mistakes:
Using unsupported :enabled pseudo-class
Checking disabled='false' attribute incorrectly
Using non-existent visible attribute
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.