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
What are locators in Selenium primarily used for? a) Automating browser settings b) Interacting with web elements c) Debugging scripts d) Executing JavaScript
Which of the following is the most efficient locator type in Selenium? a) XPath b) Name c) ID d) Tag
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
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
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
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")
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
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
Which Selenium locator uses HTML tags like <input>, <div>, or <button>? a) Class b) ID c) Tag d) XPath
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")
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
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")
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")
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")
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
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”
Which CSS Selector matches an element with both class1 and class2? a) .class1 .class2 b) .class1, .class2 c) .class1.class2 d) .class1 > .class2
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”
Which of the following XPath axes selects the parent of an element? a) ancestor b) parent c) sibling d) child
What does the * symbol represent in an XPath expression? a) Any element type b) Text nodes c) Parent element d) Attributes only
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
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
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']
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"]
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
How do you select the nth child of an element using CSS? a) :nth-child(n) b) [n] c) ::nth(n) d) :nth(n)
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
Which XPath matches elements containing the attribute data-value? a) //*[@data-value] b) //[@data-value] c) //*[data-value] d) //[@data-value=*]
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
Which is more readable and maintainable in large Selenium tests? a) CSS Selectors b) XPath c) LinkText d) TagName
Answer Key
QNo
Answer (Option with text)
1
b) Interacting with web elements
2
c) ID
3
b) To avoid ambiguous element selection
4
c) The locator becomes non-unique
5
b) Selenium throws a NoSuchElementException
6
a) driver.findElement(By.id("value"))
7
c) Selecting input fields with unique names
8
d) Selenium selects the first matching element
9
c) Tag
10
a) By.linkText("text")
11
a) Locates elements with partially matching link text
12
a) By.className("btn-primary")
13
b) driver.findElement(By.className("value"))
14
a) By.linkText("Help Center")
15
a) Yes, if they share the same tag
16
b) Elements with the id="submit"
17
c) .class1.class2
18
a) Finds all div elements with the ID “content”
19
b) parent
20
a) Any element type
21
a) #id.class
22
b) // selects anywhere, / selects from the root
23
a) //button[text()='Submit']
24
a) input[type="password"]
25
a) Finds an input element with the placeholder “Search”
26
a) :nth-child(n)
27
a) li:last-child
28
a) //*[@data-value]
29
c) Buttons containing the class “primary” anywhere