Selenium pdf
Selenium pdf
Ans. Selenium is a robust test automation suite that is used for automating web based
applications. It supports multiple browsers, programming languages and platforms.
1
2025 Edition
Selenium Interview Questions and Answers
Q.12. What is the difference between single slash(/) and double slash(//) in XPath?
Ans. In XPath a single slash is used for creating XPaths with absolute paths beginning from root
node, Whereas double slash is used for creating relative XPaths.
Ralative Xpath can identify element even though some UI changes happed, but can’t identify
by Absolute Xpath.
Q.14. What is the difference between Absolute XPath and Relative XPath?
Ans. Absolute Xpath will traverse entire HTML from the root node /html.
Relative Xpath directly jump to node based on attribute specified.
Q.15. How can we inspect the web element attributes in order to use them in different
locators?
Ans. Using Chropath or developer tools we can inspect the specific web elements. Chropath is a
plugin that provides xpaths and CSS Selectors. From automation perspective, “Right click on
page inspect element” is used specifically for inspecting web-elements in order to use their
attributes like id, class, name etc. in different locators.
Q.16. How can we locate an element by only partially matching its attributes value in
Xpath?
Ans. Using contains() method we can locate an element by partially matching its attribute's
value. This is particularly helpful in the scenarios where the attributes have dynamic values
with certain constant part.
xPath expression = //*[contains(@name,'user')]
The above statement will match the all the values of name attribute containing the word
'user' in them.
Q.19. What is the syntax of finding elements by class using CSS Selector?
Ans. By .className we can select all the element belonging to a particluar class e.g. '.inputtext '
will select all elements having class ' inputtext '.
3
2025 Edition
Selenium Interview Questions and Answers
Q.21. How can we select elements by their attribute value using CSS Selector?
Ans. Using [attribute=value] we can select all the element belonging to a particular attribute
e.g. '[type=radio]' will select the element having attribute type of value ‘radio'.
4
Selenium Interview Questions and Answers
5
Selenium Interview Questions and Answers
Q.37. How can we find the value of different attributes like name, class, value of an
element?
Ans. Using getAttribute("{attributeName}") method we can find the value of different
attrbutes of an element
e.g.- String valueAttribute = driver.findElement(By.id("elementLocator"))
.getAttribute("value");
Q.41. What are some expected conditions that can be used in Explicit waits?
Ans. Some of the commonly used expected conditions of an element that can be used with expicit
waits are-
1. elementToBeClickable(WebElement element or By locator)
2. visibilityOfElementLocated(By locator)
3. attributeContains(WebElement element, String attribute, String value)
4. alertIsPresent()
5. titleContains(String title)
6. titleIs(String title)
7. textToBePresentInElementLocated(By, String)
6
Selenium Interview Questions and Answers
.pollingEvery(Duration.ofSeconds(3))
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(new Function<WebDriver, WebElement>()
{
@Override
public WebElement apply(WebDriver driver)
{
if (text.isDisplayed())
{
return text;
} else
{
return null;
});
Q.43. What are the different keyboard operations that can be performed in selenium?
Ans. The different keyboard operations that can be performed in selenium are-
.sendKeys("sequence of characters") - Used for passing character sequence to an input or
textbox element.
.pressKey("non-text keys") - Used for keys like control, function keys etc that are non-text.
.releaseKey("non-text keys") - Used in conjunction with keypress event to simulate
releasing a key from keyboard event.
Q.44. What are the different mouse actions that can be performed?
Ans. The different mouse events supported in selenium are
1. click(WebElement element)
2. doubleClick(WebElement element)
3. contextClick(WebElement element)
4. moveToEelement(WebElement element)
5. dragAndDrop(source WebElement, target WebElement)
7
Selenium Interview Questions and Answers
8
Selenium Interview Questions and Answers
9
Selenium Interview Questions and Answers
Q.60. How can we check if an element is enabled for interaction on a web page?
Ans. Using isEnabled method we can check if an element is enabled or not.
driver.findElement(By locator).isEnabled();
Q.62. Explain the difference between implicit wait and explicit wait.?
Ans. An implicit wait, while finding an element waits for a specified time before throwing
NoSuchElementException in case element is not found. The timeout value remains valid
throughout the webDriver's instance and for all the elements.
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);
Whereas, Explicit wait is applied to a specified element only-
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(ElementLocator));
Q.63. How can we handle window UI elements and window POP ups using selenium?
Ans. Selenium is used for automating Web based application only(or browsers only). For
handling window GUI elements we can use AutoIT or Sikuli.
10
Selenium Interview Questions and Answers
Q.66. How to handle HTTPS website in selenium? or How to accept the SSL untrusted
connection?
Ans. Using profiles in firefox we can handle accept the SSL untrusted connection certificate.
Profiles are basically set of user preferences stored in a file.
Firefox
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
WebDriver driver = new FirefoxDriver(profile);
IE
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.ie.driver","IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(capabilities);
Chrome
DesiredCapabilities handlSSLErr = DesiredCapabilities.chrome ();
handlSSLErr.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver (handlSSLErr);
11
Selenium Interview Questions and Answers
12
Selenium Interview Questions and Answers
Q.84 . What is the purpose of creating a reference variable- 'driver' of type WebDriver
instead of directly creating a FireFoxDriver object or any other driver's reference in
the statement Webdriver driver = new FirefoxDriver();?
Ans. By creating a reference variable of type WebDriver we can use the same variable to work
with multiple browsers like ChromeDriver, IEDriver etc.
14
Selenium Interview Questions and Answers
1. TestNG provides different assertions that helps in checking the expected and actual
results.
2. It provides parallel execution of test methods.
3. We can define dependency of one test method over other in TestNG.
4. We can assign priority to test methods in selenium.
5. It allows grouping of test methods into test groups.
6. It allows data driven testing using @DataProvider annotation.
7. It has inherent support for reporting.
8. It has support for parameterizing test cases using @Parameters annotation.
15
Selenium Interview Questions and Answers
16
Selenium Interview Questions and Answers
17
Selenium Interview Questions and Answers
Q.92. How can we make one test method dependent on other using TestNG?
Ans. Using dependsOnMethods parameter inside @Test annotation in testNG we can make one
test method run only after successful execution of dependent test method.
@Test(dependsOnMethods = { "preTests" })
18
Selenium Interview Questions and Answers
@BefoerClass
@AfterClass
@BeforeTest
@AfterTest
@BeforeSuite
@AfterSuite
19
Selenium Interview Questions and Answers
20
Selenium Interview Questions and Answers
Q.97. Name an API used for reading and writing data to excel files.
Ans. Apache POI API and JXL (Java Excel API) can be used for reading, writing and updating excel
files.
Q.101.How can we run a Test method multiple times in a loop (without using any data
provider)?
Ans. Using invocationCount parameter and setting its value to an integer value, makes the test
method to run n number of times in a loop.
21
Selenium Interview Questions and Answers
Q.103. What is the difference between soft assertion and hard assertion in TestNG?
Ans. Soft assertions (SoftAssert) allows us to have multiple assertions within a test method,
even when an assertion fails the test method continues with the remaining test execution.
The result of all the assertions can be collated at the end using softAssert.assertAll()
method.
Here, even though the first assertion fails still the test will continue with execution and
print the message below the second assertion.
Hard assertions on the other hand are the usual assertions provided by TestNG. In case of
hard assertion in case of any failure, the test execution stops, preventing execution of any
further steps within the test method.
22
Selenium Interview Questions and Answers
Q.104. How to fail a testNG test if it doesn't get executed within a specified time?
Ans. We can use timeOut attribute of @Test annotation. The value assigned to this timeout
attribute will act as an upperbound, if test doesn't get executed within this time frame
then it will fail with timeout exception.
Q.106. How can we make sure a test method runs even if the test methods or groups on
which it depends fail or get skipped?
Ans. Using "alwaysRun" attribute of @Test annotation, we can make sure the test method will
23
Selenium Interview Questions and Answers
run even if the test methods or groups on which it depends fail or get skipped.
Here, even though the parentTest failed, the dependentTest will not get skipped instead
it will executed because of "alwaysRun=true". In case, we remove the "alwaysRun=true"
attribute from @Test then the report will show one failure and one skipped test, without
trying to run the dependentTest method.
Q.107. Why and how will you use an Excel Sheet in your project?
Ans. The reason we use Excel sheets is because it can be used as data source for tests. An excel
sheet can also be used to store the data set while performing DataDriven Testing.
Q.108. How can you redirect browsing from a browser through some proxy?
Ans. Selenium provides a PROXY class to redirect browsing from a proxy. Look at the
example below:
24
Selenium Interview Questions and Answers
Example:
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();",
element);
Q.113. Explain how you will login into any site if it is showing any authentication popup for
username and password?
Ans. Since there will be popup for logging in, we need to use the explicit command and verify if
the alert is actually present. Only if the alert is present, we need to pass the username and
password credentials.
The sample code:
25
Selenium Interview Questions and Answers
26
Selenium Interview Questions and Answers
Q.115. How To Run Failed Test Cases Using TestNG In Selenium WebDriver
Ans. By using “testng-failed.xml”
Q.117. What are different XPath functions that you have used in your Project?
Ans. Contains()
Using OR & AND
Start-with() function
Text()
27
Selenium Interview Questions and Answers
Q.121. How to click on an element which is not visible using selenium WebDriver?
Ans. We can use JavascriptExecutor to click.
WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Q.125. What are different pop-ups that you have handle in your projects?
Ans.
1. JavaScript Pop
2. Alert alert = driver.switchTo().alert();
3. Browser Pop Ups
4. Browser Profiles, Robot Class, AutoIT, Sikuli
5. Native OS Pop Ups
6. Browser Profiles, Robot Class, AutoIT, Sikuli
28
Selenium Interview Questions and Answers
Q.126. How do you handle HTTP Proxy Authentication pop ups in browser?
Ans. Form authentications URL - http://UserName:[email protected]
Example:
http://the-internet.herokuapp.com/basic_auth
https://admin:[email protected]/basic_auth
Q.130. How to find broken images in a page using Selenium Web driver.
Ans. Get xpath and then using tag name 'a'; get all the links in the page.
Use HttpURLConnector class and sent method GET
Get the response code for each link and verify if it is 404/500
// By using "href" attribute, we could get the url of the requried link
String url = element.getAttribute("href");
//System.out.println(url);
URI link = new URI(url);
29
Selenium Interview Questions and Answers
30
Selenium Interview Questions and Answers
Q.138. How many test cases you have automated per day?
Ans. It depends on Test case scenario complexity and length.
I did automate 2-5 test scenarios per day when the complexity is limited.
Sometimes just 1 or fewer test scenarios in a day when the complexity is high.
31
Selenium Interview Questions and Answers
32
Selenium Interview Questions and Answers
Q.154. How to clear the text in the text box using Selenium WebDriver?
Ans. By using clear() method
WebDriver driver = new FirefoxDriver();
driver.get("https://www.gmail.com");
driver.findElement(By.xpath("xpath_of_element1")).sendKeys("Software Testing
");
driver.findElement(By.xpath("xpath_of_element1")).clear();
Q.157. List some scenarios which we cannot automate using Selenium WebDriver?
Ans.
1. Bitmap comparison Is not possible using Selenium WebDriver
2. Automating Captcha is not possible using Selenium WebDriver
3. We can not read bar code using Selenium WebDriver
4. windows OS based pop ups
5. third party calendars/element
6. Image and Word/PDF
33
Selenium Interview Questions and Answers
Q.158. How can you use the Recovery Scenario in Selenium WebDriver?
Ans. By using “Try Catch Block” within Selenium WebDriver Java tests.
try
{
driver.get("www.xyz.com");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
Q.161. How to send an email stating the execution status to all stakeholders in Selenium?
Ans. We can send mail in Java using javax.mail library.
34
Selenium Interview Questions and Answers
int first[][] = { { 1, 2 }, { 5, 10 }, { 2, 6 } };
int second[][] = { { 2, 6 }, { 1, 2 }, { 5, 3 } };
m = first.length;
n = first[0].length;
System.out.println("Sum of 2 matrices....");
System.out.println();
}
}
// Declaration
ArrayList list = new ArrayList();
35
Selenium Interview Questions and Answers
if (num % 2 == 0)
{
System.out.println("Number is even number");
}
else
{
System.out.println("Number is odd number");
}
}
if (num % 2 == 0)
{
System.out.println("Number is even number");
}
else
{
System.out.println("Number is odd number");
}
}
36
Selenium Interview Questions and Answers
search_element = 200;
n = array.length;
first = 0;
last = n - 1;
middle = (first + last) / 2;
package LogicalPrograms;
int n = a.length;
System.out.print("Odd numbers:");
for (int i = 0; i < n; i++)
{
if (a[i] % 2 != 0)
{
37
Selenium Interview Questions and Answers
System.out.print("Even numbers:");
for (int i = 0; i < n; i++)
{
if (a[i] % 2 == 0)
{
System.out.print(a[i] + " ");
}
}
import java.util.ArrayList;
public class ArrayListExample2
{
public static void main(String[] args)
{
// Declaration
ArrayList list = new ArrayList();
38
Selenium Interview Questions and Answers
{
System.out.println(i);
}
import java.util.Random;
import java.util.Arrays;
System.out.println(Arrays.binarySearch(array, 30));
}
}
39
Selenium Interview Questions and Answers
// Sorting
temp = 0;
System.out.println();
import java.util.Scanner;
40
Selenium Interview Questions and Answers
String s = sc.nextLine();
int count = 1;
char c = 'a';
int a = 50;
int b = 100;
int c = 20;
41
Selenium Interview Questions and Answers
{
System.out.println("c is greatest");
}
{
public static void main(String[] args)
{
// Convert Integer To String Using Integer.toString() Method
int i = 123;
String s = Integer.toString(i);
System.out.println(s);
System.out.println(s);
42
Selenium Interview Questions and Answers
int c;
{
public static void main(String[] args)
{
int a = 50;
int b = 20;
if (a > b)
{
System.out.println("a is largest");
}
else
{
System.out.println("b is largest");
}
}
43
Selenium Interview Questions and Answers
int day = 5;
if (day == 1)
{
System.out.println("Sunday");
} else if (day == 2)
{
System.out.println("Monday");
} else if (day == 3)
{
System.out.println("Tuesday");
} else if (day == 4)
{
System.out.println("Wednesday");
} else if (day == 5)
{
System.out.println("Thursday");
} else if (day == 6)
{
System.out.println("Friday");
} else if (day == 7)
44
Selenium Interview Questions and Answers
{
System.out.println("Saturday");
} else
{
System.out.println("Invalid week number");
}
int count = 0;
int num = 3452;
while (num != 0)
{
num /= 10; // 345 34 3
++count;
}
a = inputNumber;
45
Selenium Interview Questions and Answers
String s = "DAD";
System.out.println(rev);
if (s.equals(rev))
{
System.out.println("Palindrome string");
} else
{
System.out.println("Not Palindrome string");
}
46
Selenium Interview Questions and Answers
if (num > 0)
{
System.out.println(" Number is Positive");
} else
{
System.out.println("Number is Negitive");
}
mport java.util.ArrayList;
import java.util.HashSet;
listWithDuplicateElements.add("JAVA");
listWithDuplicateElements.add("J2EE");
listWithDuplicateElements.add("JSP");
listWithDuplicateElements.add("SERVLETS");
listWithDuplicateElements.add("JAVA");
listWithDuplicateElements.add("STRUTS");
listWithDuplicateElements.add("JSP");
// Printing listWithDuplicateElements
System.out.println(listWithDuplicateElements);
47
Selenium Interview Questions and Answers
// Printing listWithoutDuplicateElements
System.out.println(listWithoutDuplicateElements);
}
s = s.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(s);
s1 = s1.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(s1);
System.out.println(strWithoutSpace);
}
48
Selenium Interview Questions and Answers
import java.util.Scanner;
49
Selenium Interview Questions and Answers
{
String word = words[i];
System.out.println(inputString);
System.out.println(reverseString);
System.out.println("-------------------------");
}
while (num != 0)
{
rev = rev * 10 + num % 10; // 5432
num = num / 10; // 12
}
import java.util.Scanner;
50
Selenium Interview Questions and Answers
String s=sc.nextLine();
System.out.println(rev);
for (int i : a)
{
if (num == i)
{
System.out.println("Element found");
flag = true;
break;
}
}
if (flag == false)
{
System.out.println("Element NOT found");
}
51
Selenium Interview Questions and Answers
for (int i : a)
{
if (num == i)
{
System.out.println("Element found");
flag = true;
break;
}
}
if (flag == false)
{
System.out.println("Element NOT found");
}
for (String s : a)
{
if (search_String == s)
{
System.out.println("Element found");
flag = true;
break;
}
52
Selenium Interview Questions and Answers
if (flag == false)
{
System.out.println("Element NOT found");
}
package LogicalPrograms;
for (String s : a)
{
if (search_String == s)
{
System.out.println("Element found");
flag = true;
break;
}
}
if (flag == false)
{
System.out.println("Element NOT found");
}
}
__________________________________________________________________________________________________________________
import java.util.Arrays;
System.out.println(Arrays.binarySearch(array, 10));
53
Selenium Interview Questions and Answers
}
}
int a[] = { 100, 200, 300, 400, 500 }; // Declare an array without size
and store values
for (int i : a)
{
System.out.println(i);
}
System.out.println(i);
}
import java.util.Arrays;
Arrays.sort(data);
54
Selenium Interview Questions and Answers
import java.util.Arrays;
Arrays.sort(data);
String s = "welcome";
// length()
System.out.println(s.length());
// concat()
String s1 = "welcome";
String s2 = " to java";
System.out.println(s1.concat(s2));
System.out.println("welcome".concat(" to java"));
// trim()
s = " welcome ";
55
Selenium Interview Questions and Answers
System.out.println(s);
System.out.println(s.trim());
// charAt()
s = "Welcome";
System.out.println(s.charAt(4)); // o
// Replace()
s = "welcome to java";
System.out.println(s.replace('e', 'a')); // replacing single character
System.out.println(s.replace("java", "selenium")); // replacing multiple
chars
// substring()
s = "Welcome";
System.out.println(s.substring(2, 4)); // lc
System.out.println(s.substring(4, 7)); // ome
s = "WelCome";
System.out.println(s.toLowerCase()); // welcome
System.out.println(s.toUpperCase()); // WELCOME
}
____________________________________________________________________________________
String a = "Hello";
String b = "World";
56
Selenium Interview Questions and Answers
// 1. append a and b:
a = a + b; // HelloWorld
package LogicalPrograms;
int x = 5;
int y = 10;
// x = 10, y = 5
57
Selenium Interview Questions and Answers
System.out.println(x);
System.out.println(y);
}
switch (day)
{
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid week number");
}
58
Selenium Interview Questions and Answers
a[0][0] = 100;
a[0][1] = 200;
a[1][0] = 300;
a[1][1] = 400;
a[2][0] = 500;
a[2][1] = 600;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
59
Selenium Interview Questions and Answers
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
try
{
// Creating BufferedReader object
lineCount++;
currentLine = reader.readLine();
}
60
Selenium Interview Questions and Answers
{
e.printStackTrace();
}
finally
{
try
{
reader.close(); // Closing the reader
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
61