1. Q: How do you handle dropdown/select elements using Selenium WebDriver?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select; public class DropdownHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Find the dropdown/select element
WebElement dropdown = driver.findElement(By.id("dropdown-id"));
// Create a Select object
Select select = new Select(dropdown);
// Select by visible text
select.selectByVisibleText("Option 1");
// Select by value
select.selectByValue("option-2-value");
// Select by index
select.selectByIndex(2);
// Deselect all options
select.deselectAll();
// Perform further actions on the dropdown
// ...
// Close the browser
driver.quit();
}
2. Q: How do you perform mouse hover actions using Selenium WebDriver?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions; public class MouseHover {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Find the element to hover over
WebElement element = driver.findElement(By.id("element-id"));
// Create an Actions object
Actions actions = new Actions(driver);
// Perform mouse hover action
actions.moveToElement(element).perform();
// Perform further actions after the mouse hover
// ...
// Close the browser
driver.quit();
}
3. Q: How do you capture screenshots using Selenium WebDriver?
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.apache.commons.io.FileUtils; public class ScreenshotCapture {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); // Launch
Chrome browser
WebDriver driver = new ChromeDriver(); // Navigate to a webpage
driver.get("https://example.com"); // Capture the screenshot
TakesScreenshot screenshot = (TakesScreenshot) driver;
File srcFile = screenshot.getScreenshotAs(OutputType.FILE); // Save the screenshot to a
specific location
File destFile = new File("path/to/save/screenshot.png");
FileUtils.copyFile(srcFile, destFile); // Close the browser
driver.quit();
}
4. Q: How do you perform file uploads using Selenium WebDriver?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By; public class FileUpload {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Navigate to a webpage with a file upload input
driver.get("https://example.com");
// Find the file upload input element
WebElement fileInput = driver.findElement(By.id("file-input-id"));
// Provide the file path to upload
String filePath = "path/to/file.txt";
fileInput.sendKeys(filePath);
// Perform further actions after file upload
// ...
// Close the browser
driver.quit();
}
5. Q: How do you handle frames/iframe elements using Selenium WebDriver?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; public class FrameHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Switch to a frame by index
driver.switchTo().frame(0);
// Switch to a frame by name or ID
driver.switchTo().frame("frame-name");
// Switch to a frame by WebElement
WebElement frameElement = driver.findElement(By.id("frame-id"));
driver.switchTo().frame(frameElement);
// Switch back to the default content
driver.switchTo().defaultContent();
// Perform further actions within the frame
// ...
// Close the browser
driver.quit();
}
6. Q: How do you perform scrolling actions using Selenium WebDriver?
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; public class ScrollHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Scroll vertically by pixel
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0, 500)");
// Scroll vertically to an element
WebElement element = driver.findElement(By.id("element-id"));
js.executeScript("arguments[0].scrollIntoView();", element);
// Scroll horizontally by pixel
js.executeScript("window.scrollBy(500, 0)");
// Perform further actions after scrolling
// ...
// Close the browser
driver.quit();
}
7. Q: How do you handle multiple windows/tabs using Selenium WebDriver?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; public class WindowHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Open a new window/tab
driver.get("https://example.com");
driver.switchTo().newWindow(WindowType.WINDOW);
// Get the window handles
Set<String> windowHandles = driver.getWindowHandles();
// Switch to a specific window/tab
String mainWindowHandle = driver.getWindowHandle();
for (String windowHandle : windowHandles) {
if (!windowHandle.equals(mainWindowHandle)) {
driver.switchTo().window(windowHandle);
break;
// Close the current window/tab
driver.close();
// Switch back to the main window/tab
driver.switchTo().window(mainWindowHandle);
// Perform further actions on the main window/tab
// Close the browser
driver.quit();
}
8. Q: How do you perform keyboard actions (e.g., pressing Enter, typing special characters) using
Selenium WebDriver?
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; public class KeyboardActions {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Find an input field element
WebElement inputField = driver.findElement(By.id("input-field-id"));
// Type text with keyboard actions
inputField.sendKeys("Text to type");
// Press Enter key
inputField.sendKeys(Keys.ENTER);
// Type special characters with keyboard actions
inputField.sendKeys(Keys.CONTROL, "a");
inputField.sendKeys(Keys.BACK_SPACE);
// Perform further actions after keyboard actions
// ...
// Close the browser
driver.quit();
}
9. Q: How do you handle JavaScript alerts, confirmations, and prompts using Selenium WebDriver?
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; public class JavaScriptAlerts {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Navigate to a webpage with a JavaScript alert
driver.get("https://example.com");
// Click a button that triggers a JavaScript alert
driver.findElement(By.id("alert-button")).click();
// Switch to the alert
Alert alert = driver.switchTo().alert();
// Get the text of the alert
String alertText = alert.getText();
// Accept the alert
alert.accept();
// Dismiss the alert
// alert.dismiss();
// Perform further actions after handling the alert
// ...
// Close the browser
driver.quit();
}
10. Q: How do you handle synchronization/wait conditions in Selenium WebDriver?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; public class Synchronization {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Set the maximum wait time in seconds
int waitTime = 10;
// Navigate to a webpage
driver.get("https:
//example.com");
// Wait for an element to be visible
WebDriverWait wait = new WebDriverWait(driver, waitTime);
WebElement element =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element-id")));
// Perform further actions after synchronization
// ...
// Close the browser
driver.quit();
}
11. Q: How do you handle checkboxes and radio buttons using Selenium WebDriver?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; public class CheckboxAndRadioButton {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Find a checkbox element
WebElement checkbox = driver.findElement(By.id("checkbox-id"));
// Check the checkbox if it is not selected
if (!checkbox.isSelected()) {
checkbox.click();
// Find a radio button element
WebElement radioButton = driver.findElement(By.id("radio-button-id"));
// Select the radio button if it is not selected
if (!radioButton.isSelected()) {
radioButton.click();
// Perform further actions after checkbox and radio button handling
// ...
// Close the browser
driver.quit();
}
12. Q: How do you handle pop-up windows and child windows using Selenium WebDriver?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; public class WindowHandling {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
// Click a link/button that opens a new window/pop-up
driver.findElement(By.id("new-window-button")).click();
// Get the window handles
Set<String> windowHandles = driver.getWindowHandles();
// Switch to the new window/pop-up
for (String windowHandle : windowHandles) {
driver.switchTo().window(windowHandle);
if (driver.getTitle().equals("New Window")) {
break;
// Perform actions on the new window/pop-up
// Close the new window/pop-up
driver.close();
// Switch back to the main window
String mainWindowHandle = driver.getWindowHandle();
driver.switchTo().window(mainWindowHandle);
// Perform further actions on the main window
// Close the browser
driver.quit();
}
13. Q: How do you handle cookies using Selenium WebDriver?
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; public class CookieHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Navigate to a webpage
driver.get("https:
//example.com");
// Add a cookie
Cookie cookie = new Cookie("cookie-name", "cookie-value");
driver.manage().addCookie(cookie);
// Get all cookies
Set<Cookie> cookies = driver.manage().getCookies();
// Delete a cookie
driver.manage().deleteCookie(cookie);
// Delete all cookies
driver.manage().deleteAllCookies();
// Perform further actions after cookie handling
// ...
// Close the browser
driver.quit();
}
14. Q: How do you handle dynamic elements on a webpage using Selenium WebDriver?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; public class DynamicElementHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Navigate to a webpage
driver.get("https:
//example.com");
// Wait for a dynamic element to be visible
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamic-element-id")));
// Perform further actions on the dynamic element
// ...
// Close the browser
driver.quit();
}
15. Q: How do you handle synchronization issues with Ajax calls using Selenium WebDriver?
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; public class AjaxHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Navigate to a webpage
driver.get("https:
//example.com");
// Wait for the Ajax call to complete
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.jsReturnsValue("return jQuery.active == 0"));
// Perform further actions after Ajax call completion
// ...
// Close the browser
driver.quit();
}
16. Q: How do you handle browser notifications using Selenium WebDriver?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions; public class NotificationHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create ChromeOptions instance
ChromeOptions options = new ChromeOptions();
// Disable browser notifications
options.addArguments("--disable-notifications");
// Launch Chrome browser with options
WebDriver driver = new ChromeDriver(options);
// Perform actions after disabling notifications
// ...
// Close the browser
driver.quit();
}
17. Q: How do you handle SSL certificate errors using Selenium WebDriver?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions; public class SSLCertificateHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create ChromeOptions instance
ChromeOptions options = new ChromeOptions();
// Accept SSL certificates
options.setAcceptInsecureCerts(true);
// Launch Chrome browser with options
WebDriver driver = new ChromeDriver(options);
// Perform actions after accepting SSL certificates
// ...
// Close the browser
driver.quit();
}
18. Q: How do you handle geolocation prompts using Selenium WebDriver?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions; public class GeolocationHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create ChromeOptions instance
ChromeOptions options = new ChromeOptions();
// Set geolocation coordinates
options.setExperimentalOption("geolocation", "{\"latitude\": 37.421999, \"longitude\": -
122.084}");
// Launch Chrome browser with options
WebDriver driver = new ChromeDriver(options);
// Perform actions after setting geolocation
// ...
// Close the browser
driver.quit();
}
19. Q: How do you handle file downloads using Selenium WebDriver?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions; public class FileDownloadHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create ChromeOptions instance
ChromeOptions options = new ChromeOptions();
// Set download directory
options.addArguments("--download.default_directory=/path/to/download/directory");
// Launch Chrome browser with options
WebDriver driver = new ChromeDriver(options);
// Perform actions that trigger file download
WebElement downloadButton = driver.findElement(By.id("download-button"));
downloadButton.click();
// Perform further actions after file download
// ...
// Close the browser
driver.quit();
}
20. Q: How do you handle drag and drop actions using Selenium WebDriver?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions; public class DragAndDropHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Navigate to a webpage
driver.get("https:
//example.com");
// Find the source and target elements for drag and drop
WebElement sourceElement = driver.findElement(By.id("source-element"));
WebElement targetElement = driver.findElement(By.id("target-element"));
// Perform drag and drop action
Actions actions = new Actions(driver);
actions.dragAndDrop(sourceElement, targetElement).build().perform();
// Perform further actions after drag and drop
// ...
// Close the browser
driver.quit();
}
21. Q: How do you handle dynamic dropdowns and select options using Selenium WebDriver?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select; public class DropdownHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Navigate to a webpage with a dropdown/select element
driver.get("https:
//example.com");
// Find the dropdown/select element
WebElement dropdown = driver.findElement(By.id("dropdown-id"));
// Create Select object
Select select = new Select(dropdown);
// Select an option by visible text
select.selectByVisibleText("Option 1");
// Select an option by value
select.selectByValue("option1");
// Select an option by index
select.selectByIndex(0);
// Perform further actions after dropdown handling
// ...
// Close the browser
driver.quit();
}
22. Q: How do you launch a browser using Selenium WebDriver in Java?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
```
public class BrowserLaunch {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Perform further actions on the browser
// ...
// Close the browser
driver.quit();
}
23. Q: How do you locate web elements using Selenium WebDriver?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
```
public class ElementLocators {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Find element by ID
WebElement elementById = driver.findElement(By.id("element-id"));
// Find element by name
WebElement elementByName = driver.findElement(By.name("element-name"));
// Find element by class name
WebElement elementByClassName = driver.findElement(By.className("element-class"));
// Find element by XPath
WebElement elementByXPath =
driver.findElement(By.xpath("//tagname[@attribute='value']"));
// Find element by CSS selector
WebElement elementByCssSelector =
driver.findElement(By.cssSelector("tagname[attribute='value']"));
// Find element by link text
WebElement elementByLinkText = driver.findElement(By.linkText("Link Text"));
// Find element by partial link text
WebElement elementByPartialLinkText = driver.findElement(By.partialLinkText("Partial
Link Text"));
// Perform further actions on the elements
// ...
// Close the browser
driver.quit();
}
24. Q: How do you perform actions on web elements using Selenium WebDriver?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ElementActions {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Find the element
WebElement element = driver.findElement(By.id("element-id"));
// Click on the element
element.click();
// Type text into an input field
element.sendKeys("Text to type");
// Clear the text in an input field
element.clear();
// Get the text of an element
String elementText = element.getText();
// Get the value of an attribute
String attributeValue = element.getAttribute("attribute-name");
// Perform further actions on the element
// ...
// Close the browser
driver.quit();
}
25. Q: How do you handle alerts using Selenium WebDriver?
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AlertHandling {
public static void main(String[] args) {
// Set the path of the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Launch Chrome browser
WebDriver driver = new ChromeDriver();
// Navigate to a webpage with an alert
driver.get("https://example.com");
// Click a button that triggers an alert
driver.findElement(By.id("alert-button")).click();
// Switch to the alert
Alert alert = driver.switchTo().alert();
// Get the text of the alert
String alertText = alert.getText();
// Accept the alert
alert.accept();
// Dismiss the alert
// alert.dismiss();
// Perform further actions after handling the alert
// Close the browser
driver.quit();