MCQs on Locators in Selenium | Selenium

Mastering Locators in Selenium: The Key to Efficient Web Automation
Locators in Selenium are essential for identifying and interacting with web elements. This guide covers various locator strategies like ID, Name, Class, Tag, LinkText, CSS Selectors, and XPath.


Understanding Locators

  1. What are locators in Selenium primarily used for?
    a) Automating browser settings
    b) Interacting with web elements
    c) Debugging scripts
    d) Executing JavaScript
  2. Which of the following is the most efficient locator type in Selenium?
    a) XPath
    b) Name
    c) ID
    d) Tag
  3. Why should unique locators be preferred in Selenium tests?
    a) To increase test readability
    b) To avoid ambiguous element selection
    c) To reduce code duplication
    d) To enhance script speed
  4. If two elements have the same ID on a webpage, what is a potential issue?
    a) The browser may crash
    b) Selenium throws an error
    c) The locator becomes non-unique
    d) It affects the page layout
  5. What is the result of using a non-existent locator in Selenium?
    a) Selenium skips the element
    b) Selenium throws a NoSuchElementException
    c) Selenium continues the script
    d) Selenium attempts an auto-locator

Types of Locators: ID, Name, Class, Tag, LinkText, Partial LinkText

  1. How does Selenium find an element using the id attribute?
    a) driver.findElement(By.id("value"))
    b) driver.getElementById("value")
    c) driver.id("value")
    d) driver.findById("value")
  2. What is the primary use of the name locator in Selenium?
    a) Identifying classes
    b) Selecting elements by attribute name
    c) Selecting input fields with unique names
    d) Interacting with tag names
  3. What happens if multiple elements share the same class name in Selenium?
    a) Selenium selects the last element
    b) Selenium throws an error
    c) Selenium selects all matching elements
    d) Selenium selects the first matching element
  4. Which Selenium locator uses HTML tags like <input>, <div>, or <button>?
    a) Class
    b) ID
    c) Tag
    d) XPath
  5. How can you locate a hyperlink using its exact visible text?
    a) By.linkText("text")
    b) By.partialLinkText("text")
    c) By.tagName("a")
    d) By.id("text")
  6. What is the purpose of By.partialLinkText()?
    a) Locates elements with partially matching link text
    b) Locates elements by partial CSS class
    c) Locates hyperlinks using tags
    d) Finds elements by ID and text
  7. Which locator would you use for the following code: <button class="btn-primary">Submit</button>?
    a) By.className("btn-primary")
    b) By.linkText("Submit")
    c) By.tagName("Submit")
    d) By.id("btn-primary")
  8. What is the correct syntax for using className as a locator?
    a) driver.getElementByClassName("value")
    b) driver.findElement(By.className("value"))
    c) driver.findClass("value")
    d) driver.className("value")
  9. Which locator would be appropriate for the following code: <a href="/help">Help Center</a>?
    a) By.linkText("Help Center")
    b) By.id("/help")
    c) By.name("Help Center")
    d) By.partialLinkText("help")
  10. Can By.tagName() select multiple elements?
    a) Yes, if they share the same tag
    b) No, it always selects a unique element
    c) Yes, but only if explicitly specified
    d) No, it only works with form elements

CSS Selectors and XPath Basics

  1. What does the CSS Selector #submit target?
    a) Elements with the submit class
    b) Elements with the id="submit"
    c) All buttons
    d) Elements with the name “submit”
  2. Which CSS Selector matches an element with both class1 and class2?
    a) .class1 .class2
    b) .class1, .class2
    c) .class1.class2
    d) .class1 > .class2
  3. What does the XPath expression //div[@id='content'] do?
    a) Finds all div elements with the ID “content”
    b) Selects only the first div on the page
    c) Matches div elements containing text “content”
    d) Locates div elements with a name “content”
  4. Which of the following XPath axes selects the parent of an element?
    a) ancestor
    b) parent
    c) sibling
    d) child
  5. What does the * symbol represent in an XPath expression?
    a) Any element type
    b) Text nodes
    c) Parent element
    d) Attributes only
  6. How can you locate an element using both ID and class in CSS?
    a) #id.class
    b) id.class
    c) class#id
    d) #class#id
  7. What is the difference between // and / in XPath?
    a) // selects from the root, / selects from anywhere
    b) // selects anywhere, / selects from the root
    c) // selects text only, / selects elements only
    d) They are interchangeable
  8. Which of the following is a valid XPath syntax to locate an element by text?
    a) //button[text()='Submit']
    b) //button:contains('Submit')
    c) //button['Submit']
    d) //button[text='Submit']
  9. Which CSS Selector would you use to match an input with the type="password"?
    a) input[type="password"]
    b) input.password
    c) #password
    d) input[value="password"]
  10. What does the XPath //input[@placeholder='Search'] do?
    a) Finds an input element with the placeholder “Search”
    b) Selects all input elements
    c) Matches placeholder values that start with “Search”
    d) Matches any element with “Search” in its text
  11. How do you select the nth child of an element using CSS?
    a) :nth-child(n)
    b) [n]
    c) ::nth(n)
    d) :nth(n)
  12. How would you select the last element in a list using CSS?
    a) li:last-child
    b) li:last
    c) li:last-element
    d) li:last-item
  13. Which XPath matches elements containing the attribute data-value?
    a) //*[@data-value]
    b) //[@data-value]
    c) //*[data-value]
    d) //[@data-value=*]
  14. What does the XPath //button[contains(@class, 'primary')] select?
    a) Buttons with the class “primary”
    b) Buttons that start with the class “primary”
    c) Buttons containing the class “primary” anywhere
    d) Buttons with “primary” as text
  15. Which is more readable and maintainable in large Selenium tests?
    a) CSS Selectors
    b) XPath
    c) LinkText
    d) TagName

Answer Key

QNoAnswer (Option with text)
1b) Interacting with web elements
2c) ID
3b) To avoid ambiguous element selection
4c) The locator becomes non-unique
5b) Selenium throws a NoSuchElementException
6a) driver.findElement(By.id("value"))
7c) Selecting input fields with unique names
8d) Selenium selects the first matching element
9c) Tag
10a) By.linkText("text")
11a) Locates elements with partially matching link text
12a) By.className("btn-primary")
13b) driver.findElement(By.className("value"))
14a) By.linkText("Help Center")
15a) Yes, if they share the same tag
16b) Elements with the id="submit"
17c) .class1.class2
18a) Finds all div elements with the ID “content”
19b) parent
20a) Any element type
21a) #id.class
22b) // selects anywhere, / selects from the root
23a) //button[text()='Submit']
24a) input[type="password"]
25a) Finds an input element with the placeholder “Search”
26a) :nth-child(n)
27a) li:last-child
28a) //*[@data-value]
29c) Buttons containing the class “primary” anywhere
30a) CSS Selectors

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top