0% found this document useful (0 votes)
20 views26 pages

Selenium Programación

This document provides code snippets for performing common tasks using Selenium WebDriver like handling dropdowns, mouse hover actions, capturing screenshots, file uploads, frames, and scrolling.

Uploaded by

Julian Zappia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views26 pages

Selenium Programación

This document provides code snippets for performing common tasks using Selenium WebDriver like handling dropdowns, mouse hover actions, capturing screenshots, file uploads, frames, and scrolling.

Uploaded by

Julian Zappia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

ar

um
Th
BHAVIN THUMAR
in
av
Bh

Follow: https://www.linkedin.com/in/bhavin-thumar/
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");

ar
// Launch Chrome browser

WebDriver driver = new ChromeDriver();

m
// Find the dropdown/select element

WebElement dropdown = driver.findElement(By.id("dropdown-id"));

// Create a Select object


u
Th
Select select = new Select(dropdown);

// Select by visible text

select.selectByVisibleText("Option 1");

// Select by value
in

select.selectByValue("option-2-value");
av

// Select by index

select.selectByIndex(2);

// Deselect all options


Bh

select.deselectAll();

// Perform further actions on the dropdown

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
WebDriver driver = new ChromeDriver();

// Find the element to hover over

m
WebElement element = driver.findElement(By.id("element-id"));

// Create an Actions object

u
Actions actions = new Actions(driver);
Th
// Perform mouse hover action

actions.moveToElement(element).perform();

// Perform further actions after the mouse hover

// ...
in

// Close the browser


av

driver.quit();

}
Bh

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
Chrome browser

WebDriver driver = new ChromeDriver(); // Navigate to a webpage

m
driver.get("https://example.com"); // Capture the screenshot

TakesScreenshot screenshot = (TakesScreenshot) driver;

specific location u
File srcFile = screenshot.getScreenshotAs(OutputType.FILE); // Save the screenshot to a
Th
File destFile = new File("path/to/save/screenshot.png");

FileUtils.copyFile(srcFile, destFile); // Close the browser

driver.quit();
in

}
av
Bh

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Launch Chrome browser

m
WebDriver driver = new ChromeDriver();

// Navigate to a webpage with a file upload input

driver.get("https://example.com");
u
Th
// 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";


in

fileInput.sendKeys(filePath);
av

// Perform further actions after file upload

// ...

// Close the browser


Bh

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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");

ar
// Launch Chrome browser

WebDriver driver = new ChromeDriver();

m
// Switch to a frame by index

driver.switchTo().frame(0);

// Switch to a frame by name or ID


u
Th
driver.switchTo().frame("frame-name");

// Switch to a frame by WebElement

WebElement frameElement = driver.findElement(By.id("frame-id"));

driver.switchTo().frame(frameElement);
in

// Switch back to the default content


av

driver.switchTo().defaultContent();

// Perform further actions within the frame

// ...
Bh

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
WebDriver driver = new ChromeDriver();

// Scroll vertically by pixel

m
JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("window.scrollBy(0, 500)");

// Scroll vertically to an element


u
Th
WebElement element = driver.findElement(By.id("element-id"));

js.executeScript("arguments[0].scrollIntoView();", element);

// Scroll horizontally by pixel

js.executeScript("window.scrollBy(500, 0)");
in

// Perform further actions after scrolling


av

// ...

// Close the browser

driver.quit();
Bh

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
driver.get("https://example.com");

driver.switchTo().newWindow(WindowType.WINDOW);

m
// Get the window handles

Set<String> windowHandles = driver.getWindowHandles();

// Switch to a specific window/tab


u
Th
String mainWindowHandle = driver.getWindowHandle();

for (String windowHandle : windowHandles) {

if (!windowHandle.equals(mainWindowHandle)) {

driver.switchTo().window(windowHandle);
in

break;
av

// Close the current window/tab


Bh

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();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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");

ar
// Launch Chrome browser

WebDriver driver = new ChromeDriver();

m
// Find an input field element

WebElement inputField = driver.findElement(By.id("input-field-id"));

// Type text with keyboard actions


u
Th
inputField.sendKeys("Text to type");

// Press Enter key

inputField.sendKeys(Keys.ENTER);

// Type special characters with keyboard actions


in

inputField.sendKeys(Keys.CONTROL, "a");

inputField.sendKeys(Keys.BACK_SPACE);
av

// Perform further actions after keyboard actions

// ...
Bh

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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");

ar
// Launch Chrome browser

WebDriver driver = new ChromeDriver();

m
// Navigate to a webpage with a JavaScript alert

driver.get("https://example.com");

// Click a button that triggers a JavaScript alert


u
Th
driver.findElement(By.id("alert-button")).click();

// Switch to the alert

Alert alert = driver.switchTo().alert();

// Get the text of the alert


in

String alertText = alert.getText();


av

// Accept the alert

alert.accept();

// Dismiss the alert


Bh

// alert.dismiss();

// Perform further actions after handling the alert

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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");

ar
// Launch Chrome browser

WebDriver driver = new ChromeDriver();

m
// Set the maximum wait time in seconds

int waitTime = 10;

// Navigate to a webpage
u
Th
driver.get("https:

//example.com");

// Wait for an element to be visible

WebDriverWait wait = new WebDriverWait(driver, waitTime);


in

WebElement element =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element-id")));
av

// Perform further actions after synchronization


Bh

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Launch Chrome browser

m
WebDriver driver = new ChromeDriver();

// Find a checkbox element

u
WebElement checkbox = driver.findElement(By.id("checkbox-id"));
Th
// Check the checkbox if it is not selected

if (!checkbox.isSelected()) {

checkbox.click();

}
in

// Find a radio button element


av

WebElement radioButton = driver.findElement(By.id("radio-button-id"));

// Select the radio button if it is not selected

if (!radioButton.isSelected()) {
Bh

radioButton.click();

// Perform further actions after checkbox and radio button handling

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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");

ar
// Click a link/button that opens a new window/pop-up

driver.findElement(By.id("new-window-button")).click();

m
// Get the window handles

Set<String> windowHandles = driver.getWindowHandles();

// Switch to the new window/pop-up


u
Th
for (String windowHandle : windowHandles) {

driver.switchTo().window(windowHandle);

if (driver.getTitle().equals("New Window")) {

break;
in

}
av

// Perform actions on the new window/pop-up

// Close the new window/pop-up


Bh

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();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
WebDriver driver = new ChromeDriver();

// Navigate to a webpage

m
driver.get("https:

//example.com");

// Add a cookie
u
Th
Cookie cookie = new Cookie("cookie-name", "cookie-value");

driver.manage().addCookie(cookie);

// Get all cookies

Set<Cookie> cookies = driver.manage().getCookies();


in

// Delete a cookie
av

driver.manage().deleteCookie(cookie);

// Delete all cookies

driver.manage().deleteAllCookies();
Bh

// Perform further actions after cookie handling

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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 {

ar
public static void main(String[] args) {

m
// Set the path of the ChromeDriver executable

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Launch Chrome browser


u
Th
WebDriver driver = new ChromeDriver();

// Navigate to a webpage

driver.get("https:

//example.com");
in

// Wait for a dynamic element to be visible


av

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamic-element-id")));
Bh

// Perform further actions on the dynamic element

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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) {

ar
// Set the path of the ChromeDriver executable

m
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Launch Chrome browser

u
WebDriver driver = new ChromeDriver();
Th
// Navigate to a webpage

driver.get("https:

//example.com");

// Wait for the Ajax call to complete


in

WebDriverWait wait = new WebDriverWait(driver, 10);


av

wait.until(ExpectedConditions.jsReturnsValue("return jQuery.active == 0"));

// Perform further actions after Ajax call completion


Bh

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Create ChromeOptions instance

m
ChromeOptions options = new ChromeOptions();

// Disable browser notifications

u
options.addArguments("--disable-notifications");
Th
// Launch Chrome browser with options

WebDriver driver = new ChromeDriver(options);

// Perform actions after disabling notifications


in

// ...
av

// Close the browser

driver.quit();

}
Bh

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Create ChromeOptions instance

m
ChromeOptions options = new ChromeOptions();

// Accept SSL certificates

u
options.setAcceptInsecureCerts(true);
Th
// Launch Chrome browser with options

WebDriver driver = new ChromeDriver(options);

// Perform actions after accepting SSL certificates


in

// ...
av

// Close the browser

driver.quit();

}
Bh

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Create ChromeOptions instance

m
ChromeOptions options = new ChromeOptions();

// Set geolocation coordinates

u
options.setExperimentalOption("geolocation", "{\"latitude\": 37.421999, \"longitude\": -
Th
122.084}");

// Launch Chrome browser with options

WebDriver driver = new ChromeDriver(options);

// Perform actions after setting geolocation


in

// ...
av

// Close the browser

driver.quit();
Bh

Follow: https://www.linkedin.com/in/bhavin-thumar/
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) {

ar
// Set the path of the ChromeDriver executable

m
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Create ChromeOptions instance

u
ChromeOptions options = new ChromeOptions();
Th
// Set download directory

options.addArguments("--download.default_directory=/path/to/download/directory");

// Launch Chrome browser with options

WebDriver driver = new ChromeDriver(options);


in

// Perform actions that trigger file download


av

WebElement downloadButton = driver.findElement(By.id("download-button"));

downloadButton.click();

// Perform further actions after file download


Bh

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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) {

ar
// Set the path of the ChromeDriver executable

m
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Launch Chrome browser

u
WebDriver driver = new ChromeDriver();
Th
// Navigate to a webpage

driver.get("https:

//example.com");

// Find the source and target elements for drag and drop
in

WebElement sourceElement = driver.findElement(By.id("source-element"));


av

WebElement targetElement = driver.findElement(By.id("target-element"));

// Perform drag and drop action

Actions actions = new Actions(driver);


Bh

actions.dragAndDrop(sourceElement, targetElement).build().perform();

// Perform further actions after drag and drop

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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");

ar
// Launch Chrome browser

WebDriver driver = new ChromeDriver();

m
// Navigate to a webpage with a dropdown/select element

driver.get("https:

//example.com");
u
Th
// Find the dropdown/select element

WebElement dropdown = driver.findElement(By.id("dropdown-id"));

// Create Select object

Select select = new Select(dropdown);


in

// Select an option by visible text


av

select.selectByVisibleText("Option 1");

// Select an option by value

select.selectByValue("option1");
Bh

// Select an option by index

select.selectByIndex(0);

// Perform further actions after dropdown handling

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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");

ar
// Launch Chrome browser

m
WebDriver driver = new ChromeDriver();

u
// Perform further actions on the browser

// ...
Th
// Close the browser

driver.quit();
in

}
av
Bh

Follow: https://www.linkedin.com/in/bhavin-thumar/
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

ar
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Launch Chrome browser

m
WebDriver driver = new ChromeDriver();

// Find element by ID

u
WebElement elementById = driver.findElement(By.id("element-id"));
Th
// Find element by name

WebElement elementByName = driver.findElement(By.name("element-name"));

// Find element by class name


in

WebElement elementByClassName = driver.findElement(By.className("element-class"));

// Find element by XPath


av

WebElement elementByXPath =
driver.findElement(By.xpath("//tagname[@attribute='value']"));

// Find element by CSS selector


Bh

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();

}
Follow: https://www.linkedin.com/in/bhavin-thumar/
}
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

ar
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

// Launch Chrome browser

m
WebDriver driver = new ChromeDriver();

// Find the element

u
WebElement element = driver.findElement(By.id("element-id"));
Th
// Click on the element

element.click();

// Type text into an input field

element.sendKeys("Text to type");
in

// Clear the text in an input field


av

element.clear();

// Get the text of an element

String elementText = element.getText();


Bh

// Get the value of an attribute

String attributeValue = element.getAttribute("attribute-name");

// Perform further actions on the element

// ...

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/
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");

ar
// Launch Chrome browser

WebDriver driver = new ChromeDriver();

m
// Navigate to a webpage with an alert

driver.get("https://example.com");

// Click a button that triggers an alert


u
Th
driver.findElement(By.id("alert-button")).click();

// Switch to the alert

Alert alert = driver.switchTo().alert();

// Get the text of the alert


in

String alertText = alert.getText();


av

// Accept the alert

alert.accept();

// Dismiss the alert


Bh

// alert.dismiss();

// Perform further actions after handling the alert

// Close the browser

driver.quit();

Follow: https://www.linkedin.com/in/bhavin-thumar/

You might also like