Selenium Testing Framework Selenium (WebDriver)Testing Framework 3 — Questions and Answers
Question 1: Which Select class method is used to choose an option by its visible text in a dropdown?
- selectByIndex()
- selectByValue()
- selectByVisibleText() (Correct answer)
- selectByText()
Correct answer: selectByVisibleText()
selectByVisibleText() finds the <option> element whose trimmed text matches the given string and selects it.
Question 2: What does `driver.switchTo().frame(0)` do?
- Switches to the first browser tab
- Switches to the iframe at index 0 on the page (Correct answer)
- Navigates to the first page in history
- Opens a new frame window
Correct answer: Switches to the iframe at index 0 on the page
switchTo().frame(int index) switches the WebDriver context into the iframe specified by its zero-based index.
Question 3: Which Selenium locator strategy uses an element's `name` HTML attribute?
- By.id()
- By.name() (Correct answer)
- By.tagName()
- By.className()
Correct answer: By.name()
By.name() locates elements whose name attribute matches the provided string.
Question 4: What is a StaleElementReferenceException in Selenium?
- An element was never found on the page
- A previously located element no longer exists in the DOM (Correct answer)
- The browser session has expired
- JavaScript is disabled in the browser
Correct answer: A previously located element no longer exists in the DOM
StaleElementReferenceException occurs when the DOM has been refreshed or the element removed after the WebElement reference was obtained.
Question 5: How do you perform a right-click (context click) on an element using the Actions class?
- actions.rightClick(element).perform()
- actions.contextClick(element).perform() (Correct answer)
- actions.click(element, MouseButton.RIGHT).perform()
- actions.mouseClick(element, 'right').perform()
Correct answer: actions.contextClick(element).perform()
The Actions class method contextClick() simulates a right-click on the specified WebElement.
Question 6: Which WebDriver command is used to maximize the browser window?
- driver.manage().window().maximize() (Correct answer)
- driver.window().maximize()
- driver.manage().maximize()
- driver.maximize()
Correct answer: driver.manage().window().maximize()
driver.manage().window().maximize() resizes the browser window to fill the screen.
Question 7: What does `driver.navigate().refresh()` do?
- Clears the browser cache
- Reloads the current page (Correct answer)
- Goes back to the previous page
- Opens a new tab
Correct answer: Reloads the current page
driver.navigate().refresh() reloads the currently displayed page, equivalent to pressing F5.
Which Select class method is used to choose an option by its visible text in a dropdown?