Selenium interview questions and answers
Selenium Overview
Selenium is a suite of tools used to automate Browser testing/ UI testing. Main components of Selenium are Selenium IDE, SeleniumRC/Webdriver and Selenium Grid.
Selenium IDE is an plugin with Chrome and Firefox which allows user to record user actions on browser and play back any time. And also it allows to export this generated code in different languages.
SeleniumRC (RemoteControl) is a server sits between test automation code and browser. It allows user to write the automation code with language of their choice.
Selenium RC converts these commands to Selenese commands and then sends them to the browser through HTTP requests, allowing it to control the browser’s actions.
Selenium WebDriver communicates directly with the browser using its native support, enabling more efficient and reliable interaction with web elements. Unlike Selenium RC, WebDriver does not require a server, and it supports multiple programming languages like Java, C#, Python, and Ruby. It is compatible with various browsers, including mobile browsers.
Selenium Grid is a tool that allows for parallel execution of tests across multiple browsers and operating systems. It enables the distribution of test execution by running tests on remote machines, thereby reducing the time taken to complete a test suite. Selenium Grid consists of a central hub and multiple nodes. The hub is the central point where tests are loaded, and the nodes are the machines where the tests are executed. This setup allows for efficient and scalable testing by leveraging a network of machines to run tests simultaneously, ensuring comprehensive coverage across different environments.
Selenium interview questions and answers
1. What is Selenium used for and what are the components?
Selenium is a suite of tools for automating web browsers/UI testing. Core components are Selenium IDE, Selenium RC, Selenium WebDriver, and Selenium Grid.
2. What are the advantages of Selenium Webdriver?
- Its open source and no license fee associated
- It supports multiple languages like Java, C#, PHP, Python, Perl and Ruby
- Support for multiple OS Windows, Linux and Mac OS
- Supports multiple browsers like IE, Edge, Firefox, Safari and Opera
- Selenium can integrate with various tools
- Huge developer community supported by Google
- You can find solution for any issue on the internet.. its very much matured
Its open-source, supports multiple languages, supports multiple browsers, has a large community, and can integrate with various tools.
3. How do you locate web element in Selenium?
Using locators such as ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS Selector, and XPath.
4. How to use ID to find web element?
locating web elements by their id
attribute is one of the most straightforward and efficient ways to interact with elements on a web page. The id
attribute is unique within the HTML document, making it a reliable identifier for web elements.
Selenium WebDriver class provides methods findElement(By.id(“id”)) and findElements(By.id(“id”))
5. What is the difference between findElement and findElements?
findElement
- It returns the first matching element found on the page.
- If no element is found, it throws a
NoSuchElementException
.
findElements
- It returns a list of all matching elements found on the page.
- If no element is found, it returns an empty list.
6. When do you use which element locator which one is efficient?
- By Id : It is Fast and efficient but not always available for every element. Its best to use when element has a unique Id.
- By Name: By.name(“elementname”). It is Often used in form elements but it’s not unique; multiple elements can have the same name.
- By Classname: By.className(“elementClassName”). It is useful for elements styled with unique classes but not unique; multiple elements can share the same class. Best for elements with unique classes
- By tag name: By.tagName(“tagname”). It is useful for selecting elements of a specific type but not unique; returns the first element of the specified tag.
- By linkText: By.linkText(“linkText”). It is clear and readable used when element is a link <a> with text but prone to change in the link text.
- By partialLinkText:By.partialLinkText(“partialLinkText”). It is more flexible than full link text but can match unintended elements if the partial text is common. Best used when link text is long or partially known.
- By CSS Selector: By.cssSelector(“cssSelector”). It is powerful and flexible and can combine classes, IDs, attributes, etc. But complex selectors can be harder to maintain.
- By XPath: By.xpath(“xpathExpression”), this way is very powerful and flexible and can navigate through complex DOM structures. But it can be slower than other locators and harder to read and maintain. this method is best used when elements are difficult to locate using other locators.
Each locator has its own advantages and disadvantages. Among all the locators
ID Locator is typically the best choice due to its speed and uniqueness. If id
is not available or not unique, other locators are chosen based on the context.
7. What is the difference between cssSelector and XPath selector?
-
CSS Selector:
- Cannot traverse back up the DOM (no parent or ancestor relationships).
- Limited to child and sibling relationships.
- Generally faster in execution compared to XPath.
-
XPath:
- Can navigate both forward and backward in the DOM (supports parent, ancestor, sibling, etc.).
- Supports more complex queries and conditions.
8. How would you automate a file upload using Selenium WebDriver?
Selenium cant interact with file upload dialogue. However we can achieve this with out opening the file upload dialogue given the element is of type input. Then using sendkeys we can give full file path.
9. How to check if web element is displayed on the webpage?
isDisplayed() method is used for this. It returns true if element is displayed on the page.