5. Write a complete general syntax to find an element by locator in Selenium WebDriver.
Ans: Selenium WebDriver provides two WebElement commands such as findElement, and findElements. Both commands take an input parameter By object and return an object of type WebElement.
By is an object of By class which can be used with various locator strategies such as ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, and XPATH.
WebElement elementName = driver.findElement(By.locatorName("Attribute value")); // Identify one web element within web page. List<WebElement> elementName = driver.findElements(By.locatorName("Attribute value")); // Identify list of web elements within web page.
6. How to find links on the web page?
Ans: Selenium WebDriver provides two special methods to search links on a web page.
a) Finding a link by its text:
By class of Selenium WebDriver provides linkText() method to locate the link using text displayed for link. The syntax to locate Gmail link on the Google Home page is as follows:
WebElement gmailLink = driver.findElement(By.linkText(“GMail”));
b) Finding a link by partial text:
By class of Selenium WebDriver also provides an additional method partialLinkText() to locate links using partial text. This method is useful to locate link using a fixed or known portion of the link text.
The general syntax to locate a link using the partialLinkText() method is as follows:
WebElement inboxLink = driver.findElement(By.partialLinkText(“Gmail”));
Interview Questions based on XPath in Selenium
1. What is XPath in Selenium WebDriver?
Ans: XPath stands for XML (Extensible Markup Language) Path language. It is a query language that is used to search and select nodes in an XML document. It is used for the identification of any element in an HTML or a web page.
2. Write the general syntax to find an element by XPath on web page.
Ans: The general syntax for XPath is as follows:
XPath = //tagname[@Attribute = ‘Value’]
3. What are the types of XPath in Selenium WebDriver?
Ans: There are two types of XPath in Selenium WebDriver. They are:
- Absolute XPath
- Relative XPath
4. What is Absolute XPath in Selenium WebDriver?
Ans: XPath that begins with a single forward-slash (/) and selects an element from the root HTML node is called absolute XPath.
It is the easiest and direct way to find an element but if any changes happen in the path of element, this XPath gets failed. So, this is the main disadvantage of using absolute XPath.
The general syntax to find an element using absolute XPath is as:
WebElement username = driver.findElement(By.xpath("/html/body/div/div/form/input"));
5. What are the advantages and disadvantages of absolute XPath in Selenium?
Ans: The advantage of using absolute XPath is that it identifies an element very fast.
The disadvantage of using absolute XPath is that if anything goes wrong or some other tag is added in between, this path will no longer work.
6. What is Relative XPath in Selenium WebDriver?
Ans: XPath that starts from double forward-slash (//) and selects elements from anywhere on the webpage is called relative XPath. It is the best practice to find elements through relative XPath because it helps us to reduce the chance of “ElementNotFoundException”.
The general syntax to locate an element using relative XPath is as follows:
Relative XPath: //tag-name[@Attribute = 'Value']
7. What are the advantages and disadvantages of relative XPath in Selenium WebDriver?
Ans: The advantage of using relative XPath is that you don’t have to create long XPath. You can also start from the middle.
The disadvantage of using relative XPath is that it takes more time in identifying the element as we specify the partial path, not the exact path.
8. How to find elements using attribute values in XPath?
Ans: We can search for elements using their attribute values in the XPath. Let’s take an example where we will identify username field using ID attribute.
WebElement username = driver.findElement(By.xpath("//input[@id = 'username']"));
9. What is a dynamic web element in web page?
Ans: An element whose attributes dynamically change on refresh or any other operations on the web page is called dynamic web element.
10. What are XPath axes in Selenium?
Ans: XPath axes in selenium are methods to identify those dynamic elements which are not possible to find by normal XPath method such as ID, Classname, Name, etc.
11. What are commonly useful XPath axes methods used in Selenium WebDriver?
Ans: The commonly XPath axes methods used in Selenium WebDriver are child, parent, ancestor, sibling, preceding, self, namespace, attribute, etc.
12. What is atomic values?
Ans: The node which has no parents or children is called atomic values. For example, Automation Testing, TestNG, POM.
13. What is Siblings?
Ans: The node that has the same parent is called siblings. In the above XML document, title and body elements both are siblings.
For more practice on XPath axes methods, go to this tutorial: XPath Axes in Selenium
14. How to handle Complex and Dynamic Elements in Selenium using XPath?
Ans: There are following ways by which we can handle complex and dynamic element in selenium. They are as follows:
- Basic XPath
- XPath Contains
- XPath Starts-with
- XPath Ends-with
- Using “OR” Statement
- Using “AND” Statement
- XPath Text
For more detail and Practice, go to this tutorial: XPath Contains Text
Interview Questions based on Select Class
1. What is Select in Selenium WebDriver?
Ans: Select is a class provided by Selenium which is used to work with a dropdown element. The select class allows us to select an element from the drop-down and lists that are created with the HTML <select> element.
2. What is the super class of Select class?
Ans: Object class is the super class of Select class.
3. Which interface is implemented by Select class?
Ans: Select class implements ISelect interface.
4. How to create Object of Select Class in Selenium?
Ans: Since the select is an ordinary class, its object is created by using the new keyword. After creating object, we will have to pass the dropdown WebElement as parameter to its constructor.
The syntax to create an object of select class is as follows:
Select select = new Select( WebElement element);
5. Which exception is thrown by Select class when the element is unable to be selected?
Ans: UnexpectedTagNameException will throw, when the element is not a SELECT.
6. How many types of methods are provided by Select class to select an element from the dropdown list?
Ans: The select class provides three methods to select an element from the dropdown list. They are as follows:
For more details, go to this tutorial: Select Class in Selenium | Select Class Method
7. What is the use of deselectAll(), deselectByIndex(), and deselectByValue, and deselectByVisibleText() methods?
Ans: a) deselectAll(): It is used to clear all selected entries.
b) deselectByIndex(): This method is used to deselect or clear an option at the specified index.
c) deselectByValue(): It is used for deselecting all the options whose “value” attribute matches the specified argument.
d) deselectByVisibleText(): It is used to deselect all options that display text matching the parameter.
8. Which method of Select class is used to get the first selected option from the list?
Ans: getFirstSelectedOption() method is used to get the first selected option in the list.
9. Which method should be used to check that the list supports multiple options or not?
Ans: isMultiple() method.
10. How to select dropdown value by visible text, value, and index in Selenium WebDriver?
Ans: Refer to this tutorial for coding: How to select dropdown value in Selenium.
11. How to select Multiple values in Dropdown using Selenium Webdriver?
Ans: Refer to 10.
12. How to get Selected values from Dropdown in Selenium?
Ans: getAllSelectedOptions() of select class. For coding, go to tutorial: how to select dropdown value.
13. How to deselect dropdown value in Selenium WebDriver?
Ans: For coding, go to this tutorial: Deselect dropdown value in Selenium.
Interview Questions based on Alert
1. What is an Alert?
Ans: Alert is a small dialogs message box that displays an important notification on the screen to give certain kinds of information to users or ask for permission to perform a certain kind of operation.
2. Is Alert class or Interface?
Ans: Alert is an interface.
3. What are the methods provided by Alert interface in Selenium?
Ans: Alert interface provides four methods to handle modal dialog box. They are:
a) dismiss(): It is used to clicking on the ‘Cancel’ button of the alert.
b) accept(): It is used to clicking on the ‘Ok’ button of the alert.
c) getText(): This method is used to capture alert messages.
d) sendKeys(String stringToSend): It is used to send or input data to the alert box.
Ans: For coding step by step, go to this tutorial: How to handle Alert in Selenium WebDriver.
Recommended Interview Questions
- Java Interview Questions for Selenium Automation Testing
- Top 30+ Selenium Framework Interview Questions
- Top 50+ TestNG Interview Questions and Answers
Hope that these Selenium interview questions will help you to crack interviews in the different companies. For more important interview questions, follow all links provided.
All the best!!!