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 identify an element on a web page
B. To create a test report
C. To open a new browser window
D. To close the WebDriver session

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

Introduction to Locators in Selenium WebDriver Easy
A. By.PageTitle
B. By.TextColor
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 page title string
B. A single WebElement
C. A browser object
D. A list of WebElements

4 What does findElements() return?

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

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

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

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

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

7 How can Selenium click an image element?

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

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

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

9 What action is commonly used to select a checkbox?

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

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

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

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

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

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.anchorText()
B. By.linkText()
C. By.fullLink()
D. By.exactHref()

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.shortLinkText()
B. By.partialLinkText()
C. By.containsLink()
D. By.substringText()

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. Managing browser history files
D. Creating HTML page layouts

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

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

16 What does XPath help Selenium do?

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

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. changeTo().dialog()
B. switchTo().alert()
C. open().alertBox()
D. moveTo().popup()

19 Which alert method accepts text entered by the user?

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

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. A link with bold text
C. An unsuccessful HTTP response
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.className("email")
B. By.id("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.cssSelector("[data-test='save-profile']")
B. By.className("action save-profile")
C. By.name("data-test=save-profile")
D. By.tagName("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 waits until the browser closes
B. It throws NoSuchElementException
C. It returns a list with zero items
D. It returns an empty WebElement

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).isEmpty()
B. driver.findElement(locator) == null
C. driver.findElement(locator).isEmpty()
D. driver.findElements(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.submit(); element.sendKeys("new@example.com");
C. element.sendKeys("old@example.com"); element.clear();
D. element.click(); element.getText();

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.tagName("Search")).click();
B. driver.findElement(By.cssSelector("input[alt='Search']")).click();
C. driver.findElement(By.className("input image")).click();
D. driver.findElement(By.linkText("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.isDisplayed()) box.submit();
B. if (box.isEnabled()) box.clear();
C. if (box.isSelected()) box.click();
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.name("card")
B. By.className("payment card")
C. By.cssSelector("input[name='payment'][value='card']")
D. By.cssSelector("input[name='payment']")

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).selectByName("IN");
B. new Select(dropdown).selectByVisibleText("IN");
C. new Select(dropdown).selectByIndex("IN");
D. new Select(dropdown).selectByValue("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.isSelected()
C. select.isDisplayed()
D. select.isMultiple()

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.linkText("invoice PDF")
C. By.tagName("invoice PDF")
D. By.name("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.tagName("View Details")
B. By.partialLinkText("Details")
C. By.linkText("View Details")
D. By.className("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).moveToElement(menu).perform();
B. new Actions(driver).sendKeys(menu).perform();
C. new Actions(driver).release(menu).perform();
D. new Actions(driver).clickAndHold(menu).build();

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

Action Class in Selenium WebDriver Medium
A. new Actions(driver).sendKeys(source, target).perform();
B. new Actions(driver).dragAndDrop(source, target).perform();
C. new Actions(driver).moveToElement(source, target).perform();
D. new Actions(driver).click(source).click(target).build();

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. //form[contains(@id,'Save')]//button[@id='profileForm']
B. //button[@form='profileForm' and @value='Save']
C. //form[@id='profileForm']/button[@id='Save']
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. //td[normalize-space()='ORD-105']/tr//button[normalize-space()='Edit']
B. //tr[@id='ORD-105']/td[normalize-space()='Edit']
C. //tr[td[normalize-space()='ORD-105']]//button[normalize-space()='Edit']
D. //button[normalize-space()='ORD-105']//tr[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. driver.switchTo().defaultContent(); driver.close();
C. driver.switchTo().activeElement(); driver.quit();
D. Alert alert = driver.switchTo().alert(); alert.accept();

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 switchTo().alert() and accept the newly opened browser window
B. Call navigate().forward() until the newly opened browser window becomes active
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 content type is text/html
B. The response method is HEAD
C. The response URL uses https
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 stable data-testid attribute
C. Use the button's absolute XPath
D. Use the generated id 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 null
B. Call findElement() inside a try block and ignore every exception
C. Call findElements() and assert that the returned list is empty
D. Call findElement() and compare the result with 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. Catch the exception and continue silently
B. Increase the implicit wait duration
C. Locate the collection again before clicking
D. Refresh the browser and reuse the list

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 clear() followed by sendKeys()
B. Use submit() followed by sendKeys()
C. Use getAttribute('text') followed by click()
D. Use getText() 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 hidden input's value attribute
B. Read the visible element's innerHTML only
C. Read the visible <div> using getText()
D. Read the hidden input using getCssValue('text')

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 image by its changing src
B. Locate the image using its pixel dimensions
C. Locate the parent anchor by its stable attribute
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. Wait until the overlay disappears
B. Call getText() on the image
C. Use JavaScript click immediately
D. Increase the browser window width

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 clear() before every click
B. Always call click() once
C. Call click() only when isDisplayed() is false
D. Call click() only when isSelected() is false

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 only the group container is displayed
B. Assert option B is selected and the others are not
C. Assert every radio input is selected
D. Assert the inputs share the same 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?

Select Value from DropDown using Selenium WebDriver Hard
A. Use selectByIndex(1)
B. Use selectByVisibleText('High')
C. Use selectByValue('priority-high')
D. Click the first option containing 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. Interact with the custom control and its options
B. Use selectByIndex() on the dropdown container
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.partialLinkText('View')
B. Use By.partialLinkText('report')
C. Use By.linkText('View report')
D. Use By.linkText('report')

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 partial link text with the stable phrase
B. Use an absolute XPath through every wrapper
C. Use the link's current screen coordinates
D. Use the link's full rendered HTML

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. Move to the parent, then click the submenu
B. Click the submenu before locating the parent
C. Call release() on the parent and submit the form
D. Press TAB repeatedly until the submenu appears

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. Store the coordinates before scrolling
B. Use the coordinates from a previous test
C. Move to the current WebElement and click
D. Click at (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?

XPath in Selenium WebDriver Hard
A. //td[contains(text(),'Order')]//tr/button
B. //tr[td='Order 1042']//button
C. /html/body/table/tr[2]/td[3]/button
D. //tr[contains(@class,'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. @class = 'active'
B. contains(@class, 'active')
C. contains(concat(' ', normalize-space(@class), ' '), ' active ')
D. starts-with(@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. Capture window handles, identify the new handle, then switch to it
B. Call switchTo().alert() and then window()
C. Use getWindowHandle() after switching to the new window implicitly
D. Refresh the original window and search all handles automatically

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. Dismiss every browser window after the action
B. Use a short explicit wait for alert presence and handle timeout
C. Call switchTo().window() before every assertion
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. Open every URL and mark redirects as broken
C. Check whether each anchor has a nonempty href attribute
D. Call getText() on every anchor and reject empty text