Top 50 Selenium Interview Question and Answers
-
What is Selenium?
Selenium is an open-source automation testing tool for web applications. It supports multiple programming languages, including Java, Python, and C#. -
What are the main components of Selenium?
- Selenium IDE
- Selenium WebDriver
- Selenium Grid
-
What are the advantages of using Selenium?
- Open-source
- Multi-language support
- Cross-browser and cross-platform compatibility
- Community support
-
What types of applications can Selenium test?
Selenium is used exclusively for testing web applications and cannot test desktop or mobile apps. -
What are the limitations of Selenium?
- Cannot test desktop applications
- No in-built reporting feature
- Requires third-party tools for complete automation testing (e.g., for capturing screenshots).
-
What is Selenium WebDriver?
Selenium WebDriver is an API that interacts with web browsers to automate browser actions like clicking, typing, and navigation. -
How do you start a browser session in Selenium?
WebDriver driver = new ChromeDriver();
-
What is the difference between
driver.get()
anddriver.navigate().to()
?-
driver.get()
: Waits for the page to load completely. -
driver.navigate().to()
: Does not wait for page load.
-
-
How do you handle browser windows in Selenium?
Usedriver.getWindowHandles()
to get all open windows’ handles and switch usingdriver.switchTo().window(handle)
. -
How do you handle alerts in Selenium?
Alert alert = driver.switchTo().alert(); alert.accept(); // To accept the alert
-
How do you find elements in Selenium?
Using locators like:
id
name
className
tagName
cssSelector
xpath
- What is the difference between
findElement()
andfindElements()
?
-
findElement()
: Returns the first matching element or throws an exception if none is found. -
findElements()
: Returns a list of matching elements or an empty list if none are found.
-
What are XPath and its types?
XPath is a syntax used to navigate through elements in an XML document.
-
Absolute XPath: Starts from the root (
/
). -
Relative XPath: Starts from any element (
//
).
-
How do you handle dynamic elements in Selenium?
Use dynamic XPath expressions like://*[contains(@id, 'dynamic-part')]
-
How do you perform mouse hover actions in Selenium?
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();
-
What is the Page Object Model (POM)?
POM is a design pattern where web pages are represented as classes, and elements are defined as variables in those classes. -
What is a Fluent Wait in Selenium?
Fluent Wait defines the maximum time to wait for a condition and the frequency with which to check the condition. -
How do you handle frames in Selenium?
driver.switchTo().frame("frameName");
-
What are Selenium Grid and its usage?
Selenium Grid is used for parallel test execution across different machines and browsers. -
What is the difference between implicit wait and explicit wait?
- Implicit Wait: Waits for a specified time for all elements globally.
- Explicit Wait: Waits for a specific condition for a particular element.
-
How do you integrate Selenium with TestNG?
By using annotations like@Test
,@BeforeTest
, and@AfterTest
in TestNG. -
What are the benefits of integrating Selenium with Jenkins?
- Continuous Integration and Continuous Deployment (CI/CD)
- Automated execution of Selenium tests
-
How do you capture a screenshot in Selenium?
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
-
Can Selenium be used with other testing tools?
Yes, Selenium can be integrated with JUnit, TestNG, Cucumber, and Appium. -
How do you handle SSL certificates in Selenium?
Use desired capabilities to accept insecure certificates.DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setAcceptInsecureCerts(true);
-
What should you do if an element is not clickable?
- Scroll to the element using JavaScript.
- Use explicit waits.
-
How do you handle stale element exceptions?
Refresh the element usingfindElement()
or retry the action. -
What is the cause of a NoSuchElementException?
The locator used does not match any element in the DOM. -
How do you handle timeouts in Selenium?
Use appropriate waits likeWebDriverWait
or increase the default timeout. -
What is the cause of an ElementNotVisibleException?
The element is present in the DOM but not visible on the UI. -
How do you optimize Selenium test scripts?
- Use Page Object Model.
- Minimize hard-coded waits.
- Use reusable utility methods.
-
How do you handle CAPTCHA in Selenium?
Use manual intervention or external tools like OCR. -
How do you validate text on a webpage using Selenium?
String text = driver.findElement(By.id("elementId")).getText();
-
What is headless testing in Selenium?
Running tests without a UI (e.g., using Chrome Headless or PhantomJS). -
How do you verify broken links using Selenium?
Use HTTP client libraries likeHttpURLConnection
to check response codes. -
What is the difference between Selenium 3 and Selenium 4?
Selenium 4 introduces W3C compliance, better debugging tools, and native support for Chrome DevTools Protocol. -
How do you simulate a double-click in Selenium?
Actions actions = new Actions(driver); actions.doubleClick(element).perform();
-
How do you handle dropdowns in Selenium?
Select dropdown = new Select(driver.findElement(By.id("dropdownId"))); dropdown.selectByVisibleText("Option");
-
How do you perform drag-and-drop actions in Selenium?
actions.dragAndDrop(sourceElement, targetElement).perform();
-
What are some common exceptions in Selenium?
- NoSuchElementException
- TimeoutException
- StaleElementReferenceException
- How do you handle file uploads in Selenium?
driver.findElement(By.id("uploadId")).sendKeys("filePath");
-
How do you execute JavaScript in Selenium?
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,1000)");
-
How do you verify tooltips in Selenium?
Retrieve thetitle
attribute of the element. -
How do you manage cookies in Selenium?
driver.manage().getCookies(); driver.manage().addCookie(new Cookie("key", "value"));
-
How do you close a browser in Selenium?
-
driver.close()
: Closes the current browser window. -
driver.quit()
: Closes all browser windows.
-
What is a shadow DOM, and how do you handle it in Selenium?
A shadow DOM encapsulates part of the DOM. Use JavaScript executors to access shadow DOM elements. -
What is an AutoIT tool?
AutoIT is a third-party tool used to handle file upload/download dialogs. -
How do you test responsive web designs using Selenium?
Use browser developer tools or resize the window programmatically. -
What are WebDriverWait and ExpectedConditions?
WebDriverWait waits for a specific condition defined in ExpectedConditions. -
How do you integrate Selenium tests with a CI/CD pipeline?
- Configure Jenkins or GitHub Actions.
- Add Selenium tests as a stage in the pipeline configuration.