MCQs on Advanced Locators and XPath | Selenium

Mastering advanced locators like XPath Axes, dynamic XPath techniques, and CSS selectors is essential for efficient web element identification in Selenium. Test your understanding with these insightful MCQs!


XPath Axes (Ancestor, Descendant, Following, Preceding)

  1. Which XPath axis selects all ancestors of the current node?
    a) Preceding-sibling
    b) Parent
    c) Ancestor
    d) Descendant
  2. The descendant axis in XPath refers to:
    a) Sibling nodes of an element
    b) All child and nested elements
    c) Parent elements
    d) Preceding elements
  3. How does the following axis in XPath behave?
    a) Selects all nodes after the current node in the document
    b) Selects sibling elements only
    c) Selects preceding nodes
    d) Selects parent nodes
  4. Which of the following XPath axes selects nodes in reverse document order?
    a) Following
    b) Descendant
    c) Preceding
    d) Ancestor
  5. What does the preceding-sibling axis do?
    a) Selects all previous nodes in the document
    b) Selects all nodes before the current node with the same parent
    c) Selects ancestor elements only
    d) Selects child nodes
  6. The XPath axis following-sibling is used to:
    a) Find parent elements
    b) Locate elements on the same level after the current element
    c) Select descendant nodes
    d) Select all ancestors
  7. Which axis will include all nodes from the document root to the current node?
    a) Descendant
    b) Preceding
    c) Ancestor-or-self
    d) Following
  8. How do you write an XPath to select all descendants of a node?
    a) //node/ancestor::*
    b) //node/descendant::*
    c) //node/following::*
    d) //node/preceding::*
  9. The self axis in XPath refers to:
    a) Sibling elements
    b) Current node itself
    c) Parent elements
    d) All ancestors
  10. The child axis in XPath is used to:
    a) Select only immediate child nodes of an element
    b) Select all descendant nodes
    c) Select sibling nodes
    d) Select parent nodes

Dynamic XPath Techniques

  1. What is a dynamic XPath?
    a) XPath that selects static elements
    b) XPath that adapts to dynamic web elements
    c) XPath that does not change
    d) XPath based on ID attributes
  2. The contains() function in XPath is used for:
    a) Matching exact attributes
    b) Partial matching of attributes or text
    c) Selecting child nodes
    d) Selecting preceding nodes
  3. How would you dynamically locate a button with partial text “Submit”?
    a) //button[text()='Submit']
    b) //button[contains(text(),'Submit')]
    c) //button[@id='Submit']
    d) //button[starts-with(text(),'Submit')]
  4. The starts-with() function in XPath is used to:
    a) Match elements starting with a specific attribute value
    b) Select descendant nodes
    c) Select parent nodes
    d) Select the last element
  5. Which XPath function allows combining multiple conditions?
    a) and
    b) contains()
    c) starts-with()
    d) or
  6. How would you dynamically locate an input field with a changing ID pattern like “user_123”?
    a) //input[@id='user_123']
    b) //input[starts-with(@id,'user_')]
    c) //input[@id='123']
    d) //input[ends-with(@id,'123')]
  7. What is the purpose of wildcard * in XPath?
    a) To match any attribute
    b) To match any node regardless of name
    c) To select only parent nodes
    d) To select preceding nodes
  8. Dynamic XPath can be built using which of the following?
    a) Relative paths only
    b) Wildcards, contains(), and starts-with()
    c) Absolute paths only
    d) Sibling axes
  9. Which of the following is a valid dynamic XPath for locating a link with text “Learn”?
    a) //a[@text='Learn']
    b) //a[text()='Learn']
    c) //a[contains(text(),'Learn')]
    d) //a[@link='Learn']
  10. What does normalize-space() function in XPath do?
    a) Removes duplicate elements
    b) Trims leading and trailing spaces from a string
    c) Normalizes the attribute values
    d) Combines sibling elements

CSS Selector Advanced Usage

  1. How do you locate an element using a CSS selector by ID?
    a) #id
    b) .id
    c) id
    d) @id
  2. The CSS selector .class1 .class2 selects:
    a) Elements with both class1 and class2
    b) Nested elements with class2 inside elements with class1
    c) Elements with either class1 or class2
    d) All elements with class1
  3. Which CSS selector matches elements by attribute and value?
    a) [attribute='value']
    b) @attribute='value'
    c) (attribute=value)
    d) {attribute='value'}
  4. How do you use CSS selectors to match elements containing a specific word in an attribute?
    a) [attribute=value*]
    b) [attribute~=value]
    c) [attribute|=value]
    d) [attribute^=value]
  5. The > symbol in CSS selectors indicates:
    a) A descendant relationship
    b) A direct child relationship
    c) A sibling relationship
    d) An ancestor relationship
  6. How do you select the first child of an element using CSS?
    a) :nth-child(0)
    b) :nth-of-type(1)
    c) :nth-child(1)
    d) :first-child
  7. What does * represent in CSS selectors?
    a) All parent elements
    b) All elements
    c) All sibling elements
    d) All input elements
  8. Which of the following matches elements that start with a specific attribute value in CSS?
    a) [attribute^='value']
    b) [attribute*='value']
    c) [attribute$='value']
    d) [attribute~='value']
  9. How do you select an element with multiple classes in CSS?
    a) .class1,.class2
    b) .class1 .class2
    c) .class1.class2
    d) .class1@.class2
  10. Which CSS selector matches elements ending with a specific value in an attribute?
    a) [attribute^='value']
    b) [attribute$='value']
    c) [attribute*='value']
    d) [attribute|='value']

Here are the answers for the remaining questions:

QnoAnswer
1c) Ancestor
2b) All child and nested elements
3a) Selects all nodes after the current node in the document
4c) Preceding
5b) Selects all nodes before the current node with the same parent
6b) Locate elements on the same level after the current element
7c) Ancestor-or-self
8b) //node/descendant::*
9b) Current node itself
10a) Select only immediate child nodes of an element
11b) XPath that adapts to dynamic web elements
12b) Partial matching of attributes or text
13b) //button[contains(text(),'Submit')]
14a) Match elements starting with a specific attribute value
15a) and
16b) //input[starts-with(@id,'user_')]
17b) To match any node regardless of name
18b) Wildcards, contains(), and starts-with()
19c) //a[contains(text(),'Learn')]
20b) Trims leading and trailing spaces from a string
21a) #id
22b) Nested elements with class2 inside elements with class1
23a) [attribute='value']
24a) [attribute=value*]
25b) A direct child relationship
26c) :nth-child(1)
27b) All elements
28a) [attribute^='value']
29c) .class1.class2
30b) [attribute$='value']

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