Unit 3: Selenium Locators - Practice Quiz
1 What is the main purpose of a locator in Selenium WebDriver?
2 Which of the following is a valid Selenium locator strategy?
3
What does findElement() return when it successfully locates an element?
4
What does findElements() return?
5 Which WebElement method is commonly used to enter text into a form field?
sendKeys()
submitText()
clearAll()
getText()
6 Which WebElement method removes existing text from an input field?
eraseText()
clear()
remove()
deleteValue()
7 How can Selenium click an image element?
pressImage() on the driver
tapPicture() on the page
selectImage() on the browser
click() on the image WebElement
8 Which method checks whether a checkbox or radio button is selected?
isSelected()
hasSelection()
isCheckedState()
getChecked()
9 What action is commonly used to select a checkbox?
10 Which Selenium class is commonly used for a standard HTML select dropdown?
Dropdown
OptionList
Select
ChoiceBox
11 Which method selects a dropdown option by its visible text?
selectByCaption()
selectByVisibleText()
pickByDisplayedText()
chooseByLabel()
12 Which locator matches the complete visible text of a link?
By.exactHref()
By.fullLink()
By.linkText()
By.anchorText()
13 Which locator can find a link using only part of its visible text?
By.substringText()
By.containsLink()
By.partialLinkText()
By.shortLinkText()
14
What is the Selenium Actions class mainly used for?
15
Which interaction can be performed using the Selenium Actions class?
16 What does XPath help Selenium do?
17 Which symbol commonly begins an absolute XPath expression?
/
#
%
@
18 Which WebDriver method switches focus to a JavaScript alert?
switchTo().alert()
moveTo().popup()
open().alertBox()
changeTo().dialog()
19 Which alert method accepts text entered by the user?
enterText()
writeAlert()
sendKeys()
typeValue()
20 What usually indicates a broken link on a web page?
21
A page contains <input id="email" name="userEmail">. The id value is unique and stable. Which locator is most appropriate?
By.id("email")
By.className("email")
By.name("email")
By.tagName("email")
22
Several buttons share the class action, but only the required button has data-test="save-profile". Which locator targets it most reliably?
By.className("action save-profile")
By.name("data-test=save-profile")
By.tagName("save-profile")
By.cssSelector("[data-test='save-profile']")
23
What happens when driver.findElement(By.id("status")) cannot find a matching element?
WebElement
NoSuchElementException
24 A test must check whether any validation messages are present without throwing an exception when none exist. Which approach is suitable?
driver.findElements(locator) == null
driver.findElements(locator).isEmpty()
driver.findElement(locator).isEmpty()
driver.findElement(locator) == null
25
An input already contains old@example.com. Which sequence replaces it with new@example.com?
element.clear(); element.sendKeys("new@example.com");
element.sendKeys("old@example.com"); element.clear();
element.click(); element.getText();
element.submit(); element.sendKeys("new@example.com");
26
A form has no visible submit button, but a located text field belongs to that form. Which WebElement method can submit the enclosing form?
click()
sendKeys()
submit()
isEnabled()
27
A clickable image is represented by <input type="image" alt="Search">. Which statement clicks it using its accessible attribute?
driver.findElement(By.cssSelector("input[alt='Search']")).click();
driver.findElement(By.linkText("Search")).click();
driver.findElement(By.className("input image")).click();
driver.findElement(By.tagName("Search")).click();
28 A checkbox should be selected, but the test must not deselect it if it is already selected. Which code is correct?
if (box.isSelected()) box.click();
if (box.isEnabled()) box.clear();
if (box.isDisplayed()) box.submit();
if (!box.isSelected()) box.click();
29
Three radio buttons have the same name="payment" and different value attributes. Which locator selects only the card option?
By.className("payment card")
By.name("card")
By.cssSelector("input[name='payment']")
By.cssSelector("input[name='payment'][value='card']")
30
Given <option value="IN">India</option>, which code selects the option based on the submitted value?
new Select(dropdown).selectByVisibleText("IN");
new Select(dropdown).selectByValue("IN");
new Select(dropdown).selectByIndex("IN");
new Select(dropdown).selectByName("IN");
31
A <select multiple> element allows several choices. Which method verifies that multiple selection is supported before selecting several options?
select.isEnabled()
select.isMultiple()
select.isDisplayed()
select.isSelected()
32
A link is displayed as Download monthly invoice PDF, where the month may be inserted dynamically. Which locator is most resilient to the changing month?
By.partialLinkText("invoice PDF")
By.tagName("invoice PDF")
By.name("invoice PDF")
By.linkText("invoice PDF")
33
Two links contain the text Details, but only one has the exact visible text View Details. Which locator specifically matches the exact text?
By.className("View Details")
By.tagName("View Details")
By.partialLinkText("Details")
By.linkText("View Details")
34 A submenu appears only after hovering over a menu element. Which Java Selenium code performs the hover?
new Actions(driver).sendKeys(menu).perform();
new Actions(driver).clickAndHold(menu).build();
new Actions(driver).moveToElement(menu).perform();
new Actions(driver).release(menu).perform();
35
Which statement performs a drag-and-drop operation from source to target?
new Actions(driver).dragAndDrop(source, target).perform();
new Actions(driver).sendKeys(source, target).perform();
new Actions(driver).click(source).click(target).build();
new Actions(driver).moveToElement(source, target).perform();
36
Which XPath locates a Save button within the form whose id is profileForm, without matching Save buttons elsewhere?
//button[@form='profileForm' and @value='Save']
//form[@id='profileForm']/button[@id='Save']
//form[contains(@id,'Save')]//button[@id='profileForm']
//form[@id='profileForm']//button[normalize-space()='Save']
37
A table row contains a cell with text ORD-105. Which XPath finds the Edit button in that same row?
//tr[td[normalize-space()='ORD-105']]//button[normalize-space()='Edit']
//tr[@id='ORD-105']/td[normalize-space()='Edit']
//button[normalize-space()='ORD-105']//tr[normalize-space()='Edit']
//td[normalize-space()='ORD-105']/tr//button[normalize-space()='Edit']
38 After clicking a delete button, a JavaScript confirmation alert appears. Which sequence accepts it and then returns control to page elements?
Alert alert = driver.switchTo().alert(); alert.dismiss();
Alert alert = driver.switchTo().alert(); alert.accept();
driver.switchTo().activeElement(); driver.quit();
driver.switchTo().defaultContent(); driver.close();
39 Clicking a link opens a second browser window. After saving the original handle, what is the correct way to switch to the new window?
navigate().forward() until the newly opened browser window becomes active
switchTo().alert() and accept the newly opened browser window
switchTo().frame(1) because each new window is represented as a frame
getWindowHandles() and switch to the handle unequal to the original
40
A test collects anchor href values and sends HTTP requests to identify broken links. Which result most strongly indicates a broken link?
https
text/html
HEAD
404
41
A page contains two buttons with identical visible text and stable data-testid values, but their generated id attributes change after every deployment. Which locator strategy is most robust for selecting the second button?
id attribute
data-testid attribute
42 A test must verify that no error banners are displayed. The locator may match zero, one, or several elements. Which implementation avoids an exception when the count is zero?
findElements() and assert that the returned list is empty
findElement() inside a try block and ignore every exception
findElement() and compare the result with null
findElements() and assert that the returned list is null
43
A collection is located with findElements(), and the page re-renders the collection before the test clicks the third item. The click fails with StaleElementReferenceException. What is the best correction?
44
A form input is initially populated with "Old value", and the test must replace it reliably even when the application selects existing text inconsistently. Which sequence is most appropriate?
getAttribute('text') followed by click()
submit() followed by sendKeys()
getText() followed by sendKeys()
clear() followed by sendKeys()
45
A custom form control is visually rendered as a <div>, while the actual submitted value is maintained by a hidden <input>. Which action best validates the value that will be submitted?
<div> using getText()
getCssValue('text')
value attribute
innerHTML only
46
An image is wrapped by an anchor, has no meaningful alt text, and receives the click handler on its parent element. Which locator is most semantically appropriate for activating the image action?
src
47
A clickable image is present in the DOM but initially covered by a loading overlay. A direct click() throws ElementClickInterceptedException. What should the test do first?
getText() on the image
48 A checkbox may be selected by default, and the test must ensure it is selected without accidentally toggling it off. Which logic is correct?
click() only when isSelected() is false
click() only when isDisplayed() is false
click() once
clear() before every click
49
A radio group contains three inputs with the same name, and one option is selected. Which assertion most accurately verifies the group state after choosing option B?
id value
50
A native HTML <select> contains duplicate visible labels but distinct value attributes. The test must choose the option whose value is priority-high. Which method is most precise?
selectByIndex(1)
selectByVisibleText('High')
High
selectByValue('priority-high')
51
A control styled as a dropdown is implemented with a <div> and <li> elements rather than a native <select>. What is the correct approach?
selectByIndex() on the dropdown container
Select on the first <li> element
Select
52
A page contains links named View report, View report history, and Preview report. The test must select only View report. Which locator is safest?
By.linkText('View report')
By.linkText('report')
By.partialLinkText('report')
By.partialLinkText('View')
53
A link's visible label includes a nested <span> and whitespace inserted by responsive rendering. Exact link text is unreliable, but a stable unique phrase occurs in the label. Which strategy is preferable?
54 A hover menu appears only after the pointer moves over a parent element. The test then needs to click a submenu item. Which sequence correctly models the interaction?
TAB repeatedly until the submenu appears
release() on the parent and submit the form
55
An element is partially outside the viewport, and a click using Actions targets its center after scrolling. Which implementation best avoids using stale coordinates?
(0, 0) after scrolling
56
A table contains rows with dynamic class names. The test must click the button in the row whose first cell is exactly Order 1042. Which XPath is most appropriate?
/html/body/table/tr[2]/td[3]/button
//td[contains(text(),'Order')]//tr/button
//tr[contains(@class,'Order 1042')]//button
//tr[td='Order 1042']//button
57
An element has a class attribute containing multiple tokens, and the test must match the token active without also matching inactive. Which XPath condition is correct?
contains(@class, 'active')
contains(concat(' ', normalize-space(@class), ' '), ' active ')
starts-with(@class, 'active')
@class = 'active'
58 A click opens a new browser window and no JavaScript alert is present. Which sequence correctly switches to the new window before locating an element inside it?
getWindowHandle() after switching to the new window implicitly
switchTo().alert() and then window()
59 A page may show a confirmation alert only for certain test data. The test should accept it when present but continue normally when absent. Which handling is appropriate?
switchTo().window() before every assertion
switchTo().alert().accept()
60
A test extracts anchor href values to find broken links. Which validation most accurately detects an HTTP failure while avoiding false positives from non-HTTP links?
http or https URLs and inspect the response status
getText() on every anchor and reject empty text
href attribute
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →