Selenium WebDriver Core API & Commands 1 β Questions and Answers
Question 1: Which WebDriver method is used to navigate to a URL?
- driver.get(url) (Correct answer)
- driver.open(url)
- driver.navigate(url)
- driver.load(url)
Correct answer: driver.get(url)
driver.get(url) loads a new web page in the current browser window.
Question 2: What does driver.findElement(By.id('foo')) return if no element is found?
- NoSuchElementException (Correct answer)
- null
- an empty WebElement
- ElementNotFoundException
Correct answer: NoSuchElementException
WebDriver throws a NoSuchElementException when findElement cannot locate a matching element.
Question 3: Which method retrieves the current page title in Selenium WebDriver?
- driver.getTitle() (Correct answer)
- driver.title()
- driver.pageTitle()
- driver.getPageTitle()
Correct answer: driver.getTitle()
driver.getTitle() returns the title of the current page as a String.
Question 4: How do you close only the current browser window (not the entire session) in WebDriver?
- driver.close() (Correct answer)
- driver.quit()
- driver.exit()
- driver.closeWindow()
Correct answer: driver.close()
driver.close() closes the focused browser window; driver.quit() ends the entire WebDriver session.
Question 5: What does driver.getWindowHandle() return?
- A unique string ID for the current window (Correct answer)
- A list of all open window IDs
- The window title
- The window index
Correct answer: A unique string ID for the current window
driver.getWindowHandle() returns a String that uniquely identifies the current browser window.
Question 6: Which By strategy locates elements using CSS selectors?
- By.cssSelector() (Correct answer)
- By.css()
- By.style()
- By.selector()
Correct answer: By.cssSelector()
By.cssSelector() accepts any valid CSS selector string to locate elements on the page.
Which WebDriver method is used to navigate to a URL?