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!
Which XPath axis selects all ancestors of the current node? a) Preceding-sibling b) Parent c) Ancestor d) Descendant
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
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
Which of the following XPath axes selects nodes in reverse document order? a) Following b) Descendant c) Preceding d) Ancestor
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
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
Which axis will include all nodes from the document root to the current node? a) Descendant b) Preceding c) Ancestor-or-self d) Following
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::*
The self axis in XPath refers to: a) Sibling elements b) Current node itself c) Parent elements d) All ancestors
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
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
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
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')]
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
Which XPath function allows combining multiple conditions? a) and b) contains() c) starts-with() d) or
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')]
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
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
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']
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
How do you locate an element using a CSS selector by ID? a) #id b) .id c) id d) @id
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
Which CSS selector matches elements by attribute and value? a) [attribute='value'] b) @attribute='value' c) (attribute=value) d) {attribute='value'}
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]
The > symbol in CSS selectors indicates: a) A descendant relationship b) A direct child relationship c) A sibling relationship d) An ancestor relationship
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
What does * represent in CSS selectors? a) All parent elements b) All elements c) All sibling elements d) All input elements
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']
How do you select an element with multiple classes in CSS? a) .class1,.class2 b) .class1 .class2 c) .class1.class2 d) .class1@.class2
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:
Qno
Answer
1
c) Ancestor
2
b) All child and nested elements
3
a) Selects all nodes after the current node in the document
4
c) Preceding
5
b) Selects all nodes before the current node with the same parent
6
b) Locate elements on the same level after the current element
7
c) Ancestor-or-self
8
b) //node/descendant::*
9
b) Current node itself
10
a) Select only immediate child nodes of an element
11
b) XPath that adapts to dynamic web elements
12
b) Partial matching of attributes or text
13
b) //button[contains(text(),'Submit')]
14
a) Match elements starting with a specific attribute value
15
a) and
16
b) //input[starts-with(@id,'user_')]
17
b) To match any node regardless of name
18
b) Wildcards, contains(), and starts-with()
19
c) //a[contains(text(),'Learn')]
20
b) Trims leading and trailing spaces from a string
21
a) #id
22
b) Nested elements with class2 inside elements with class1