Q61. What is the difference between driver.findElement() and driver.findElements()?
Answer:
- findElement() returns the first matching WebElement based on the locator and throws NoSuchElementException if not found.
- findElements() returns a list of matching WebElements. If no element is found, it returns an empty list.
java
CopyEdit
WebElement element = driver.findElement(By.id(“username”));
List<WebElement> elements = driver.findElements(By.className(“inputField”));
๐น Q62. What is the difference between Implicit Wait and Explicit Wait?
Answer:
- Implicit Wait: Applies globally. Tells the WebDriver to wait for a certain amount of time before throwing an exception.
- Explicit Wait: Applies to a specific element until a condition is met (like visibility or clickability).
java
// Implicit
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Explicit
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“searchBox”)));
๐น Q63. How to handle window-based popups in Selenium?
Answer:
Selenium only works with web-based applications. For Windows popups, use tools like:
- AutoIT
- Sikuli
๐น Q64. What is the Robot API in Selenium?
Answer:
Robot API is used to simulate keyboard or mouse events, often for file uploads or alerts not handled by Selenium.
java
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
๐น Q65. How can you upload a file using Selenium?
Answer:
- Use sendKeys(“file path”) on the file input field.
- Use Robot API, AutoIT, or Sikuli when input type is not accessible.
java
driver.findElement(By.id(“upload”)).sendKeys(“C:\\path\\to\\file.txt”);
๐น Q66. How to handle SSL Certificate errors in Selenium?
Answer:
Use browser profiles or capabilities:
Chrome:
java
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(cap);
๐น Q67. How to perform Drag and Drop in Selenium?
Answer:
Use the Actions class.
java
Actions act = new Actions(driver);
act.dragAndDrop(source, target).build().perform();
๐น Q68. How to execute JavaScript in Selenium?
Answer: Use JavascriptExecutor.
java
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“alert(‘Hello’);”);
๐น Q69. How to handle Alerts in Selenium?
Answer: Switch to alert and use .accept() or .dismiss().
java
Alert alert = driver.switchTo().alert();
alert.accept(); // OK
alert.dismiss(); // Cancel
๐น Q70. What is HtmlUnitDriver?
Answer:
HtmlUnitDriver is a headless browser driver that runs tests without a GUI. It’s fast and useful for backend automation.
๐น Q71. How to interact with hidden elements in Selenium?
Answer:
Use JavaScriptExecutor:
java
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“document.getElementById(‘hiddenElement’).click();”);
๐น Q72. What is Page Object Model (POM)?
Answer:
POM is a design pattern where each page is represented by a class that contains its elements and behaviors.
๐น Q73. What are the advantages of POM?
Answer:
- Improves code reusability and maintainability
- Separates test logic from UI locators
- Easy updates if UI changes
๐น Q74. What is Page Factory?
Answer:
Page Factory is a way to implement POM using @FindBy annotations and PageFactory.initElements().
java
@FindBy(id=”username”) WebElement userInput;
PageFactory.initElements(driver, this);
๐น Q75. What is an Object Repository?
Answer:
An Object Repository is a central location to store web elements, typically implemented using POM or Page Factory.
๐น Q76. What is a Data-Driven Framework?
Answer:
In Data-Driven Framework, test data is stored separately in Excel, CSV, etc. and used to drive test logic.
๐น Q77. What is a Keyword-Driven Framework?
Answer:
In Keyword-Driven Framework, keywords define actions (like openBrowser, clickButton), separated from code.
๐น Q78. What is a Hybrid Framework?
Answer:
Hybrid Framework is a combination of Data-Driven and Keyword-Driven approaches for better flexibility.
๐น Q79. What is Selenium Grid?
Answer:
Selenium Grid allows parallel test execution across multiple machines/browsers using a Hub-Node architecture.
๐น Q80. What are the advantages of Selenium Grid?
Answer:
- Parallel execution
- Multi-browser testing
- Cross-platform testing
๐น Q81. What is a Hub in Selenium Grid?
Answer:
Hub is the central server in Selenium Grid that distributes test cases to different Nodes.
๐น Q82. What is a Node in Selenium Grid?
Answer:
Nodes are the remote machines with different OS/browsers where tests are executed.
๐น Q83. What does this line mean: WebDriver driver = new FirefoxDriver();?
Answer:
It means you’re creating a WebDriver reference and initializing it with FirefoxDriver, which controls the Firefox browser.
๐น Q84. Why do we use WebDriver driver = new FirefoxDriver(); instead of FirefoxDriver driver = new FirefoxDriver();?
Answer:
It allows cross-browser testing using a single WebDriver reference, enabling flexibility.
๐น Q85. What is TestNG?
Answer:
TestNG is a testing framework that supports annotations, parallel test execution, data-driven testing, and better reporting.