Selenium WebDriver Technology & Digital Applications 5 β Questions and Answers
Question 1: What is the advantage of using CSS selectors over XPath locators in Selenium WebDriver?
- CSS selectors are generally faster and more readable for attribute-based lookups (Correct answer)
- CSS selectors support text-content matching, which XPath does not
- CSS selectors work only in Chrome while XPath is universal
- CSS selectors can traverse up the DOM tree like XPath axes
Correct answer: CSS selectors are generally faster and more readable for attribute-based lookups
CSS selectors are typically evaluated faster by browser engines and are considered more readable for class, ID, and attribute-based element selection.
Question 2: In Selenium, what is the correct method to retrieve the text content of a web element?
- element.getText() (Correct answer)
- element.getAttribute('textContent')
- element.getValue()
- element.getInnerHTML()
Correct answer: element.getText()
element.getText() returns the visible text of the element as rendered in the browser, trimming leading/trailing whitespace.
Question 3: What does the implicit wait setting in Selenium WebDriver affect?
- It tells WebDriver how long to poll the DOM when trying to find an element before throwing NoSuchElementException (Correct answer)
- It sets the maximum time allowed for a page to fully load
- It defines the timeout for JavaScript execution
- It sets the delay between each keystroke during sendKeys()
Correct answer: It tells WebDriver how long to poll the DOM when trying to find an element before throwing NoSuchElementException
Implicit wait configures a global timeout that WebDriver uses to repeatedly search for an element before declaring it not found.
Question 4: Which Selenium WebDriver class provides the ability to simulate file uploads by sending the file path to an input element?
- WebElement.sendKeys() with the absolute file path (Correct answer)
- FileUploader utility class
- Robot class with file dialog handling
- Actions.dragAndDropBy() with file reference
Correct answer: WebElement.sendKeys() with the absolute file path
For file input elements, calling sendKeys() with the absolute path of the file directly sets the file path without needing to interact with the OS file dialog.
Question 5: What is the primary purpose of Selenium WebDriver's FluentWait compared to WebDriverWait?
- FluentWait allows custom polling intervals and the ability to ignore specific exception types during waiting (Correct answer)
- FluentWait waits for multiple conditions simultaneously while WebDriverWait handles one
- FluentWait is faster because it uses browser-native events instead of polling
- FluentWait automatically retries the failed assertion three times before throwing
Correct answer: FluentWait allows custom polling intervals and the ability to ignore specific exception types during waiting
FluentWait extends WebDriverWait and adds the ability to configure the polling frequency and specify exception types to ignore during the wait period.
Question 6: When Selenium WebDriver executes driver.navigate().refresh(), what happens to the current browser session?
- The current page is reloaded without ending the WebDriver session (Correct answer)
- The browser closes and reopens the page in a new session
- All cookies are cleared and the page reloads
- The browser navigates to the page's canonical URL
Correct answer: The current page is reloaded without ending the WebDriver session
driver.navigate().refresh() reloads the currently displayed page, equivalent to pressing F5, while keeping the WebDriver session and browser state intact.
Question 7: What is the difference between findElement() and findElements() in Selenium WebDriver?
- findElement() returns the first matching WebElement or throws NoSuchElementException; findElements() returns a List that is empty if none match (Correct answer)
- findElement() searches the entire DOM; findElements() searches only the visible portion
- findElements() is faster because it uses a cached DOM snapshot
- findElement() accepts only ID locators; findElements() accepts all locator types
Correct answer: findElement() returns the first matching WebElement or throws NoSuchElementException; findElements() returns a List that is empty if none match
findElement() throws NoSuchElementException when no match exists, while findElements() safely returns an empty List, making it useful for conditional checks.
What is the advantage of using CSS selectors over XPath locators in Selenium WebDriver?