Selenium Testing Framework Selenium (WebDriver)Testing Framework 5 — Questions and Answers
Question 1: In Selenium Grid 4, what is the role of the 'Hub'?
- Executes the tests on the local machine
- Acts as a central router that distributes test sessions to registered nodes (Correct answer)
- Stores test results and screenshots
- Provides the browser binaries to remote machines
Correct answer: Acts as a central router that distributes test sessions to registered nodes
The Hub receives WebDriver requests from clients and routes each session to an available Node that matches the requested capabilities.
Question 2: Which XPath expression selects a button element that contains the text 'Submit'?
- //button[@text='Submit']
- //button[contains(text(),'Submit')] (Correct answer)
- //button{text='Submit'}
- //button.text('Submit')
Correct answer: //button[contains(text(),'Submit')]
The XPath function contains(text(),'Submit') matches any button whose text node includes the string 'Submit'.
Question 3: What is the difference between `findElement()` and `findElements()` in WebDriver?
- findElement() returns a List; findElements() returns a single WebElement
- findElement() throws NoSuchElementException if not found; findElements() returns an empty list (Correct answer)
- findElements() is faster than findElement()
- findElement() supports only ID locators; findElements() supports all locators
Correct answer: findElement() throws NoSuchElementException if not found; findElements() returns an empty list
findElement() throws NoSuchElementException when no match exists, while findElements() silently returns an empty List<WebElement>.
Question 4: Which Selenium 4 feature allows you to capture a screenshot of a specific WebElement rather than the full page?
- element.takeScreenshot()
- element.getScreenshotAs(OutputType.FILE) (Correct answer)
- driver.getScreenshotOf(element)
- driver.captureElement(element)
Correct answer: element.getScreenshotAs(OutputType.FILE)
In Selenium 4, WebElement implements TakesScreenshot, so element.getScreenshotAs(OutputType.FILE) captures just that element's area.
Question 5: Which technique is used to wait for an AJAX call to complete before interacting with dynamically loaded elements?
- Thread.sleep() with a fixed delay
- Explicit wait with an appropriate ExpectedCondition (Correct answer)
- Implicit wait set to 0 seconds
- driver.navigate().refresh() after the AJAX call
Correct answer: Explicit wait with an appropriate ExpectedCondition
Explicit waits using WebDriverWait and ExpectedConditions poll until the AJAX result appears in the DOM, avoiding hard-coded sleeps.
Question 6: What does the `getAttribute('innerHTML')` call return for a WebElement?
- The element's CSS class list
- The raw HTML content nested inside the element (Correct answer)
- The element's tag name
- The element's computed style
Correct answer: The raw HTML content nested inside the element
getAttribute('innerHTML') returns the HTML markup contained within the element's opening and closing tags.
Question 7: In TestNG integrated with Selenium, which annotation marks a method that runs once before all tests in a class?
- @BeforeMethod
- @BeforeTest
- @BeforeClass (Correct answer)
- @BeforeSuite
Correct answer: @BeforeClass
@BeforeClass annotates a method that executes once before the first test method in the current class, commonly used to instantiate the WebDriver.
In Selenium Grid 4, what is the role of the 'Hub'?