Monday, July 15, 2024

Selenium Interview Questions and Answers - PART 2

1. WebDriver is interface or class?
  • WebDriver is an interface in Selenium.
2. If we do not add a driver exe file what will happen and what kind of exception will be generated?
  • If the driver executable is not added, Selenium won't be able to communicate with the browser, and a WebDriverException will be thrown.
3. What is the difference between close and quit?
  • close(): Closes the current browser window.
  • quit(): Closes all the browser windows and ends the WebDriver session.
4. Difference between get and navigate().to() ?
  • get(): Loads a new web page in the current browser window.
  • navigate().to(): This does the same as get() but allows for additional navigation options like back, forward, and refresh.
5. Difference between findElement and findElements?
  • findElement: Returns a single WebElement or throws  NoSuchElementException if not found.
  • findElements: Returns a list of WebElements. If no elements are found, it returns an empty list.
6. How do you get all the links on the current page? Which locator will you use other than XPath?
  • You can use the CSS selector a to find all links.
List<WebElement> links = driver.findElements(By.cssSelector("a"));

7. Methods of WebDriver?
  • Some common methods are: get() , getCurrentUrl() , getTitle(), findElement() , findElements() , getPageSource() , close() , quit(), navigate() , manage() .
8. What is the use of the getCurrentPageSource method?
  • It returns the source code of the current page.
9. When do we go for the findElements method and what is the return type?
  • Use findElements when you expect multiple elements. It returns a list of WebElements.
10. Why do we get WebDriverException?
  • This exception is thrown when WebDriver is unable to interact with the browser. Possible reasons include incorrect WebDriver setup, browser crashes, or network issues.
11. Absolute and Relative XPath?
  • Absolute XPath: Starts from the root and follows a complete path (e.g., /html/body/div ).
  • Relative XPath: Starts from the middle of the HTML DOM structure (e.g., //div[@id='example'] ).

No comments:

Post a Comment