0
0
Selenium Javatesting~5 mins

findElement by cssSelector in Selenium Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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'?
A"#active"
B"active"
C".active"
D"*active"
What does the CSS selector '#submitBtn' select?
AElements with id 'submitBtn'
BElements with name 'submitBtn'
CAll buttons
DElements with class 'submitBtn'
What happens if findElement(By.cssSelector()) finds no matching element?
AThrows NoSuchElementException
BReturns an empty list
CReturns null
DReturns a default element
Which CSS selector finds all input elements inside a form?
A"form > input"
B"form input"
C"input form"
D"form + input"
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"
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.