Unit 3: Selenium Locators - Practice Quiz

CSE377 — Web Automation Testing 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is the main purpose of a locator in Selenium WebDriver?

Introduction to Locators in Selenium WebDriver Easy
A. To close the WebDriver session
B. To create a test report
C. To identify an element on a web page
D. To open a new browser window

2 Which of the following is a valid Selenium locator strategy?

Introduction to Locators in Selenium WebDriver Easy
A. By.TextColor
B. By.PageTitle
C. By.Id
D. By.FontSize

3 What does findElement() return when it successfully locates an element?

FindElement and FindElements in Selenium WebDriver Easy
A. A browser object
B. A list of WebElements
C. A page title string
D. A single WebElement

4 What does findElements() return?

FindElement and FindElements in Selenium WebDriver Easy
A. Only the first matching element
B. The active browser alert
C. The current browser URL
D. A collection of matching elements

5 Which WebElement method is commonly used to enter text into a form field?

Selenium Form WebElement Easy
A. sendKeys()
B. submitText()
C. clearAll()
D. getText()

6 Which WebElement method removes existing text from an input field?

Selenium Form WebElement Easy
A. eraseText()
B. clear()
C. remove()
D. deleteValue()

7 How can Selenium click an image element?

Clicking on Image in Selenium WebDriver Easy
A. Call pressImage() on the driver
B. Use tapPicture() on the page
C. Call selectImage() on the browser
D. Call click() on the image WebElement

8 Which method checks whether a checkbox or radio button is selected?

Selecting CheckBox and Radio Button in Selenium WebDriver Easy
A. isSelected()
B. hasSelection()
C. isCheckedState()
D. getChecked()

9 What action is commonly used to select a checkbox?

Selecting CheckBox and Radio Button in Selenium WebDriver Easy
A. Read the page source
B. Change the window size
C. Refresh the browser
D. Click the checkbox

10 Which Selenium class is commonly used for a standard HTML select dropdown?

Select Value from DropDown using Selenium WebDriver Easy
A. Dropdown
B. OptionList
C. Select
D. ChoiceBox

11 Which method selects a dropdown option by its visible text?

Select Value from DropDown using Selenium WebDriver Easy
A. selectByCaption()
B. selectByVisibleText()
C. pickByDisplayedText()
D. chooseByLabel()

12 Which locator matches the complete visible text of a link?

Locate Elements by Link Text and Partial Link Text in Selenium WebDriver Easy
A. By.exactHref()
B. By.fullLink()
C. By.linkText()
D. By.anchorText()

13 Which locator can find a link using only part of its visible text?

Locate Elements by Link Text and Partial Link Text in Selenium WebDriver Easy
A. By.substringText()
B. By.containsLink()
C. By.partialLinkText()
D. By.shortLinkText()

14 What is the Selenium Actions class mainly used for?

Action Class in Selenium WebDriver Easy
A. Storing test case results
B. Performing advanced user interactions
C. Creating HTML page layouts
D. Managing browser history files

15 Which interaction can be performed using the Selenium Actions class?

Action Class in Selenium WebDriver Easy
A. CSS compilation
B. Mouse hovering
C. Database backup
D. Browser installation

16 What does XPath help Selenium do?

XPath in Selenium WebDriver Easy
A. Download browser drivers
B. Generate Java classes
C. Locate elements in an HTML document
D. Measure internet speed

17 Which symbol commonly begins an absolute XPath expression?

XPath in Selenium WebDriver Easy
A. /
B. #
C. %
D. @

18 Which WebDriver method switches focus to a JavaScript alert?

Alert and Popup Window Handling in Selenium WebDriver Easy
A. switchTo().alert()
B. moveTo().popup()
C. open().alertBox()
D. changeTo().dialog()

19 Which alert method accepts text entered by the user?

Alert and Popup Window Handling in Selenium WebDriver Easy
A. enterText()
B. writeAlert()
C. sendKeys()
D. typeValue()

20 What usually indicates a broken link on a web page?

Finding Broken Links using Selenium WebDriver Easy
A. A page with a large heading
B. An unsuccessful HTTP response
C. A link with bold text
D. A link with an image

21 A page contains <input id="email" name="userEmail">. The id value is unique and stable. Which locator is most appropriate?

Introduction to Locators in Selenium WebDriver Medium
A. By.id("email")
B. By.className("email")
C. By.name("email")
D. 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?

Introduction to Locators in Selenium WebDriver Medium
A. By.className("action save-profile")
B. By.name("data-test=save-profile")
C. By.tagName("save-profile")
D. By.cssSelector("[data-test='save-profile']")

23 What happens when driver.findElement(By.id("status")) cannot find a matching element?

FindElement and FindElements in Selenium WebDriver Medium
A. It returns an empty WebElement
B. It returns a list with zero items
C. It waits until the browser closes
D. It throws NoSuchElementException

24 A test must check whether any validation messages are present without throwing an exception when none exist. Which approach is suitable?

FindElement and FindElements in Selenium WebDriver Medium
A. driver.findElements(locator) == null
B. driver.findElements(locator).isEmpty()
C. driver.findElement(locator).isEmpty()
D. driver.findElement(locator) == null

25 An input already contains old@example.com. Which sequence replaces it with new@example.com?

Selenium Form WebElement Medium
A. element.clear(); element.sendKeys("new@example.com");
B. element.sendKeys("old@example.com"); element.clear();
C. element.click(); element.getText();
D. 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?

Selenium Form WebElement Medium
A. click()
B. sendKeys()
C. submit()
D. isEnabled()

27 A clickable image is represented by <input type="image" alt="Search">. Which statement clicks it using its accessible attribute?

Clicking on Image in Selenium WebDriver Medium
A. driver.findElement(By.cssSelector("input[alt='Search']")).click();
B. driver.findElement(By.linkText("Search")).click();
C. driver.findElement(By.className("input image")).click();
D. 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?

Selecting CheckBox and Radio Button in Selenium WebDriver Medium
A. if (box.isSelected()) box.click();
B. if (box.isEnabled()) box.clear();
C. if (box.isDisplayed()) box.submit();
D. 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?

Selecting CheckBox and Radio Button in Selenium WebDriver Medium
A. By.className("payment card")
B. By.name("card")
C. By.cssSelector("input[name='payment']")
D. By.cssSelector("input[name='payment'][value='card']")

30 Given <option value="IN">India</option>, which code selects the option based on the submitted value?

Select Value from DropDown using Selenium WebDriver Medium
A. new Select(dropdown).selectByVisibleText("IN");
B. new Select(dropdown).selectByValue("IN");
C. new Select(dropdown).selectByIndex("IN");
D. 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 Value from DropDown using Selenium WebDriver Medium
A. select.isEnabled()
B. select.isMultiple()
C. select.isDisplayed()
D. 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?

Locate Elements by Link Text and Partial Link Text in Selenium WebDriver Medium
A. By.partialLinkText("invoice PDF")
B. By.tagName("invoice PDF")
C. By.name("invoice PDF")
D. 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?

Locate Elements by Link Text and Partial Link Text in Selenium WebDriver Medium
A. By.className("View Details")
B. By.tagName("View Details")
C. By.partialLinkText("Details")
D. By.linkText("View Details")

34 A submenu appears only after hovering over a menu element. Which Java Selenium code performs the hover?

Action Class in Selenium WebDriver Medium
A. new Actions(driver).sendKeys(menu).perform();
B. new Actions(driver).clickAndHold(menu).build();
C. new Actions(driver).moveToElement(menu).perform();
D. new Actions(driver).release(menu).perform();

35 Which statement performs a drag-and-drop operation from source to target?

Action Class in Selenium WebDriver Medium
A. new Actions(driver).dragAndDrop(source, target).perform();
B. new Actions(driver).sendKeys(source, target).perform();
C. new Actions(driver).click(source).click(target).build();
D. 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?

XPath in Selenium WebDriver Medium
A. //button[@form='profileForm' and @value='Save']
B. //form[@id='profileForm']/button[@id='Save']
C. //form[contains(@id,'Save')]//button[@id='profileForm']
D. //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?

XPath in Selenium WebDriver Medium
A. //tr[td[normalize-space()='ORD-105']]//button[normalize-space()='Edit']
B. //tr[@id='ORD-105']/td[normalize-space()='Edit']
C. //button[normalize-space()='ORD-105']//tr[normalize-space()='Edit']
D. //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 and Popup Window Handling in Selenium WebDriver Medium
A. Alert alert = driver.switchTo().alert(); alert.dismiss();
B. Alert alert = driver.switchTo().alert(); alert.accept();
C. driver.switchTo().activeElement(); driver.quit();
D. 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?

Alert and Popup Window Handling in Selenium WebDriver Medium
A. Call navigate().forward() until the newly opened browser window becomes active
B. Call switchTo().alert() and accept the newly opened browser window
C. Call switchTo().frame(1) because each new window is represented as a frame
D. Iterate through 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?

Finding Broken Links using Selenium WebDriver Medium
A. The response URL uses https
B. The response content type is text/html
C. The response method is HEAD
D. The response status code is 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?

Introduction to Locators in Selenium WebDriver Hard
A. Use the visible button text alone
B. Use the button's absolute XPath
C. Use the generated id attribute
D. Use the stable 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?

FindElement and FindElements in Selenium WebDriver Hard
A. Call findElements() and assert that the returned list is empty
B. Call findElement() inside a try block and ignore every exception
C. Call findElement() and compare the result with null
D. Call 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?

FindElement and FindElements in Selenium WebDriver Hard
A. Refresh the browser and reuse the list
B. Increase the implicit wait duration
C. Catch the exception and continue silently
D. Locate the collection again before clicking

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?

Selenium Form WebElement Hard
A. Use getAttribute('text') followed by click()
B. Use submit() followed by sendKeys()
C. Use getText() followed by sendKeys()
D. Use 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?

Selenium Form WebElement Hard
A. Read the visible <div> using getText()
B. Read the hidden input using getCssValue('text')
C. Read the hidden input's value attribute
D. Read the visible element's 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?

Clicking on Image in Selenium WebDriver Hard
A. Locate the parent anchor by its stable attribute
B. Locate the image using its pixel dimensions
C. Locate the image by its changing src
D. Locate the image by absolute XPath

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?

Clicking on Image in Selenium WebDriver Hard
A. Use JavaScript click immediately
B. Call getText() on the image
C. Increase the browser window width
D. Wait until the overlay disappears

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?

Selecting CheckBox and Radio Button in Selenium WebDriver Hard
A. Call click() only when isSelected() is false
B. Call click() only when isDisplayed() is false
C. Always call click() once
D. Call 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?

Selecting CheckBox and Radio Button in Selenium WebDriver Hard
A. Assert every radio input is selected
B. Assert option B is selected and the others are not
C. Assert the inputs share the same id value
D. Assert only the group container is displayed

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?

Select Value from DropDown using Selenium WebDriver Hard
A. Use selectByIndex(1)
B. Use selectByVisibleText('High')
C. Click the first option containing High
D. Use 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?

Select Value from DropDown using Selenium WebDriver Hard
A. Use selectByIndex() on the dropdown container
B. Interact with the custom control and its options
C. Use Select on the first <li> element
D. Wrap the container with Selenium 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?

Locate Elements by Link Text and Partial Link Text in Selenium WebDriver Hard
A. Use By.linkText('View report')
B. Use By.linkText('report')
C. Use By.partialLinkText('report')
D. Use 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?

Locate Elements by Link Text and Partial Link Text in Selenium WebDriver Hard
A. Use the link's current screen coordinates
B. Use the link's full rendered HTML
C. Use partial link text with the stable phrase
D. Use an absolute XPath through every wrapper

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?

Action Class in Selenium WebDriver Hard
A. Click the submenu before locating the parent
B. Press TAB repeatedly until the submenu appears
C. Move to the parent, then click the submenu
D. Call 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?

Action Class in Selenium WebDriver Hard
A. Move to the current WebElement and click
B. Click at (0, 0) after scrolling
C. Store the coordinates before scrolling
D. Use the coordinates from a previous test

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?

XPath in Selenium WebDriver Hard
A. /html/body/table/tr[2]/td[3]/button
B. //td[contains(text(),'Order')]//tr/button
C. //tr[contains(@class,'Order 1042')]//button
D. //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?

XPath in Selenium WebDriver Hard
A. contains(@class, 'active')
B. contains(concat(' ', normalize-space(@class), ' '), ' active ')
C. starts-with(@class, 'active')
D. @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?

Alert and Popup Window Handling in Selenium WebDriver Hard
A. Use getWindowHandle() after switching to the new window implicitly
B. Capture window handles, identify the new handle, then switch to it
C. Refresh the original window and search all handles automatically
D. Call 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?

Alert and Popup Window Handling in Selenium WebDriver Hard
A. Call switchTo().window() before every assertion
B. Use a short explicit wait for alert presence and handle timeout
C. Dismiss every browser window after the action
D. Always call 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?

Finding Broken Links using Selenium WebDriver Hard
A. Send an HTTP request only for http or https URLs and inspect the response status
B. Call getText() on every anchor and reject empty text
C. Check whether each anchor has a nonempty href attribute
D. Open every URL and mark redirects as broken