Top 10 Essential Selenium WebDriver Code Snippets for Interview Preparation

1. How do you launch a browser using Selenium WebDriver?

Answer:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LaunchBrowser {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com");
        driver.quit();
    }
}

2. How do you locate an element by ID and enter text into it?

Answer:

WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
driver.findElement(By.id("username")).sendKeys("myUsername");
driver.quit();

3. How do you click a button using Selenium WebDriver?

Answer:

WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
driver.findElement(By.id("submitButton")).click();
driver.quit();

4. How do you handle a dropdown menu in Selenium WebDriver?

Answer:

import org.openqa.selenium.support.ui.Select;

WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
Select dropdown = new Select(driver.findElement(By.id("dropdownMenu")));
dropdown.selectByVisibleText("Option1");
driver.quit();

5. How do you handle JavaScript alerts in Selenium WebDriver?

Answer:

WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
driver.findElement(By.id("alertButton")).click();
Alert alert = driver.switchTo().alert();
alert.accept();  // To accept the alert
// alert.dismiss();  // To dismiss the alert
driver.quit();

6. How do you take a screenshot in Selenium WebDriver?

Answer:

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("path/to/screenshot.png"));
driver.quit();

7. How do you perform mouse hover action in Selenium WebDriver?

Answer:

import org.openqa.selenium.interactions.Actions;

WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
Actions actions = new Actions(driver);
WebElement menu = driver.findElement(By.id("menu"));
actions.moveToElement(menu).perform();
driver.quit();

8. How do you handle multiple windows in Selenium WebDriver?

Answer:

WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
String mainWindowHandle = driver.getWindowHandle();
driver.findElement(By.id("newWindowButton")).click();
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(mainWindowHandle)) {
        driver.switchTo().window(handle);
        // Perform actions in the new window
        driver.close();
    }
}
driver.switchTo().window(mainWindowHandle);
driver.quit();

9. How do you execute JavaScript using Selenium WebDriver?

Answer:

import org.openqa.selenium.JavascriptExecutor;

WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("alert('Executing JavaScript in Selenium WebDriver');");
driver.quit();

10. How do you wait for an element to be visible in Selenium WebDriver?

Answer:

import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
element.sendKeys("myUsername");
driver.quit();

Leave a Reply

Your email address will not be published. Required fields are marked *