July 25, 2025

Selenium Java Interview Questions and Answers with Selenium 4 code

Here are my Selenium Java Interview Questions and Answers. If you want my complete set of 100+ Selenium Java Questions and Answers as a document, you can message me on LinkedIn at Inder P Singh.



Question: What is Selenium?
Answer: suite of tools for automating web browsers, to automate interactions and verify expected behavior

Question: What are the Selenium components?
Answer: Selenium IDE, Selenium WebDriver, and Selenium Grid

Question: What is Selenium IDE?
Answer: Browser extension or add-on (Chrome, Firefox & Edge) to record scripts, which can run on any browser or Selenium Grid. Used for bug reproduction scripts and website exploration.

Question: How does Selenium WebDriver work?
Answer: It interacts with a web browser or a remote web server. It sends commands to a browser and retrieves the results.

Question: What is the latest version of Selenium WebDriver?
Answer: version 4 (Find element(s) methods, Capabilities and Actions class are different from version 3)

Question: What is Selenium Grid?
Answer: Tool to run Selenium tests on multiple machines simultaneously, reducing the time to test the web application on multiple browsers or operating systems

Question: What are the Selenium advantages?
Answer: free to use, supports multiple programming languages e.g. Java, Python, C#, supports multiple browsers e.g. Chrome, Firefox (each browser has it's own driver)

Question: What are the Selenium limitations?
Answer: no support for testing desktop applications, limited support for testing mobile applications, no inbuilt reporting features

Question: How to get the correct browser driver for a Selenium WebDriver session in Java?
Answer: download the correct browser driver or by using WebDriverManager e.g.
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
  
Question: How to create a Selenium WebDriver session in Java?
Answer: by using WebDriver interface e.g.
WebDriver driver = new ChromeDriver();
  
Question: How to create a local Selenium WebDriver in Java?
Answer: by giving path to driver executable and using WebDriver interface e.g.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
Question: How to create a remote Selenium WebDriver in Java?
Answer: by using RemoteWebDriver class with ChromeOptions e.g.
ChromeOptions options = new ChromeOptions();
WebDriver driver = new RemoteWebDriver(new URL("https://inderpsingh.blogspot.com/"), options);
Question: In Selenium Java, browser navigation can be done by which command(s)?
Answer: driver.get(), driver.navigate().to(), driver.navigate().back(), driver.navigate().forward() and driver.navigate().refresh();

Question: How to maximize the browser window?
Answer: driver.manage().window().maximize();
Example: You can follow me on LinkedIn at https://www.linkedin.com/in/inderpsingh/

Question: How to get the web page title and the URL in the address bar?
Answer: driver.getTitle(); driver.getCurrentUrl();

Question: How to verify the expected result?
Answer: By using JUnit e.g.
Assert.assertTrue(driver.getTitle().contains("Software and Testing Training"));
Question: What are the different locator strategies in Selenium WebDriver with Java?
Answer: WebDriver can find element By id, By name, By class name, By tag name, By link text, By partial link text, By CSS selector or By XPath

Question: Why are relative XPaths preferred instead of absolute XPaths as locators of elements?
Answer: Relative XPaths are more stable than absolute (complete) XPaths, which fail if any part of the path changes even slightly.

Question: What is InvalidSelectorException in Selenium WebDriver with Java?
Answer: It is thrown when an invalid selector is used to locate an element. To fix this issue, check the selector to see if it is correct and that it is being used correctly.

Question: Relative locators in Selenium 4?
Answer: They identify the location, above, below, toLeftOf, toRightOf or near (less than 50 px) another Web Element with a simple(r) locator.
By submitLocator = RelativeLocator.withTagName("button").below(By.id("simpleID"));
Question: How do you chain relative locators in Selenium 4?
Answer: Chaining relative locators means using multiple relative locators e.g.
By login = RelativeLocator.withTagName(By.tagName("button")).below(By.id("Password")).toRightOf(By.id("Cancel"));
Question: How do you interact with an input box in Selenium WebDriver with Java?
Answer:
WebElement i = driver.findElement(By.id("inputId"));
  i.sendKeys("some value" + Keys.ENTER);
Question: How do you clear an input box in Selenium WebDriver with Java?
Answer: By using the clear method. Note it only works if the input element is enabled and editable. e.g.
WebElement i = driver.findElement(By.id("inputId"));
i.clear();
Question: What is the difference between WebDriver close() and quit() methods?
Answer: close() method closes the main browser window but the quit() method (recommended) closes all the browser windows and deletes the session.

Question: How to write the if statement in Java?
Answer: 1) if (condition) { // code block to run if the condition is true } else { // code block to run if the condition is false}
2) variable = (condition) ? expressionTrue : expressionFalse;

Question: How to execute some JavaScript in Selenium with Java?
Answer:
import org.openqa.selenium.JavascriptExecutor;
JavascriptExecutor js = (JavascriptExecutor) driver;
// open a blank tab in the browser
js.executeScript("window.open();");
Question: How do you interact with a link in Selenium WebDriver with Java?
Answer:
WebElement l = driver.findElement(By.linkText("Link Text"));
l.click();
Question: What if the link text is long or can change?
Answer: It is better to use partialLinkText with the stable part of the link text e.g.
WebDriverWait w = new WebDriverWait(driver, 30);
WebElement l = w.until(ExpectedConditions.elementToBeClickable(
  By.partialLinkText("Partial Link Text")));
l.click();
Question: Which element will WebDriver find if there are multiple matching elements for the locator, as in
WebElement a = driver.findElement(By.className("answer"));
Answer: The first matching web element in the DOM

Question: How to find the 2nd or another element instead of the first element?
Answer: By limiting the scope of finding it e.g.
WebElement question2 = driver.findElement(By.id("q2"));
WebElement a = question2.findElement(By.className("answer"));
Question: How to find the 2nd element using a single WebDriver command?
Answer: By using CSS selector or XPath with # for ID and . for class name e.g.
WebElement a = driver.findElement(By.cssSelector("#q2 .answer"));
Question: Which WebDriver command works on any web element?
Answer: click – it clicks the web element in the middle of the element.

Question: How to get the text shown in the browser by an element?
Answer: By using the getText() method e.g.
String t = driver.findElement(By.cssSelector("#q1")).getText();
Question: How to get some attribute's value of a web element?
Answer: By using the getAttribute() method e.g.
String answer = driver.findElement(By.name("answer1")).getAttribute("value");
Question: When invoked on a web element, what does the submit() method do?
Answer: It is used to submit a form. It works on any web element. But in Selenium 4, it is recommended to click the specific form submission button.

Question: How do you handle dropdowns?
Answer: by using Select class
WebElement dropdown = driver.findElement(By.id("dropdown"));
Select s = new Select(dropdown);
s.selectByVisibleText("Option 1");
Question: How to know if a dropdown is multi select dropdown?
Answer: HTML: the select tag contains "multiple" attribute.
d.isMultiple();
Question: Which method works for the multi select dropdown but NOT for the single select dropdown?
Answer: deselectByVisibleText()

Question: How do you verify the state e.g. visible or enabled of a web element?
Answer: By using .isDisplayed() or .isEnabled() methods on the element. .isSelected() method is to verify if a check box, radio button and so on is selected.

Question: How do you handle alerts?
Answer: by using Alert class
Alert a = driver.switchTo().alert();
a.accept();
  
Question: What is NoAlertPresentException?
Answer: It is thrown when there is no alert present on the page. To fix this, verify that an alert is present before interacting with it. Also, follow me in LinkedIn at https://www.linkedin.com/in/inderpsingh/

Question: How do you handle confirmations?
Answer: by using Alert class, a confirmation may be accepted (OK) or dismissed (Cancel)
Alert c = driver.switchTo().alert();
c.dismiss();
  
Question: How can WebDriver handle the JavaScript popup with 2 buttons?
Answer: These are Confirmation (Confirm box) and Prompt alert. Both have accept() (for OK) and dismiss() (for Cancel) methods. Prompt has sendKeys() for text input.

Question: How do you handle frames in Selenium?
Answer: iframes (elements from any domain), switch to iframe e.g.
driver.switchTo().frame("frameName");
  
Question: What is element not found exception in Selenium with Java?
Answer: It can occur when the web element is not present on the web page (e.g. DOM not loaded fully) or not interactable or is within an iframe or shadow root or the element's locator has changed.

Question: How do you interact with an element in an iframe?
Answer:
driver.switchTo().frame("iframeId");
WebElement element = driver.findElement(By.id("elementId"));
element.click();
  
Question: In Selenium with Java, how can you switch to an iframe with duplicate ID & name?
Answer: driver.switchTo().frame() switches to the first iframe with the duplicate ID or name. driver.findElement() can find the iframe as a WebElement to switch to it. The frame index can also be used to switch to it.

Question: In Selenium with Java, what is NoSuchFrameException?
Answer: It is thrown when the frame you are trying to switch to is not present on the page. To fix this, verify that the frame is present on the page before attempting to switch to it.

Question: How to print the data of all the iframes on a web page?
Answer:
for (WebElement f : driver.findElements(By.tagName("iframe"))) {
    String frameData = "iframe id=" + f.getAttribute("id")
        + ", name=" + f.getAttribute("name")
        + ", source=" + f.getAttribute("src");
    System.out.println(frameData);
}
  
Question: What is a window handle?
Answer: It is a unique alphanumeric string of every window. The window handle is generated and controlled by the browser for a single session.

I have explained all these questions and answers and many more Selenium Java questions and answers in my video below. Please view it. Thank you!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.