Â
Please note that the Selenium methods mentioned below are just a selection of commonly used methods, and many more are available in Selenium WebDriver for Java.Â
Refer to the below methods for Interview Preparation.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
Â
// Assuming you have already initialized a WebDriver instance, for example:
WebDriver driver = new ChromeDriver();
Â
// Finding Elements
WebElement element = driver.findElement(By.<locator_strategy>("<locator_value>"));
List<WebElement> elements = driver.findElements(By.<locator_strategy>("<locator_value>"));
Â
// Interacting with Elements
element.click(); // Clicks on the element
element.sendKeys("<text>"); // Types text into the element
element.clear(); // Clears the text in the element
Â
// Accessing Element Information
String text = element.getText(); // Retrieves the text of the element
String attributeValue = element.getAttribute("<attribute_name>"); // Retrieves the value of the specified attribute
element.isDisplayed(); // Checks if the element is visible on the page
element.isEnabled(); // Checks if the element is enabled
element.isSelected(); // Checks if the element is selected (for checkboxes and radio buttons)
Â
// Element Interaction (Mouse Actions)
Actions actions = new Actions(driver);
actions.moveToElement(element).perform(); // Moves the mouse pointer to the element
actions.contextClick(element).perform(); // Performs a right-click on the element
actions.doubleClick(element).perform(); // Performs a double-click on the element
actions.dragAndDrop(source, target).perform(); // Drags the source element and drops it on the target element
Â
// Waiting for Elements
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Implicitly waits for elements to be present
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.<expected_condition>(By.<locator_strategy>("<locator_value>"))); // Explicitly waits for a specific condition to be met
Â
// Switching Between Frames and Windows
driver.switchTo().frame("<frame_name>"); // Switches to a frame using its name or ID
driver.switchTo().defaultContent(); // Switches back to the default content
driver.switchTo().window("<window_handle>"); // Switches to a window using its handle
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString()); // Switches to a newly opened window
Â
// Executing JavaScript
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("<javascript_code>"); // Executes JavaScript code in the context of the current page
Â
// Interacting with Select Elements (Dropdowns)
Select select = new Select(element);
select.selectByVisibleText("<visible_text>"); // Selects an option by its visible text
select.selectByValue("<value>"); // Selects an option by its value attribute
select.selectByIndex(<index>); // Selects an option by its index (starting from 0)
select.deselectAll(); // Deselects all selected options
select.deselectByVisibleText("<visible_text>"); // Deselects an option by its visible text
select.deselectByValue("<value>"); // Deselects an option by its value attribute
select.deselectByIndex(<index>); // Deselects an option by its index
Â
// Uploading Files
element.sendKeys("<file_path>"); // Uploads a file by specifying its local file path
Â
// Handling Windows and Tabs
String mainWindowHandle = driver.getWindowHandle(); // Retrieves the handle of the main window
Set<String> windowHandles = driver.getWindowHandles(); // Retrieves the handles of all open windows
driver.switchTo().window("<window_handle>"); // Switches to a window using its handle
driver.switchTo().defaultContent(); // Switches back to the default content
Â
// Executing Asynchronous Scripts
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.<expected_condition>(<arguments>)); // Waits for a specific condition to be met
Â
// Closing the Browser
driver.close(); // Closes the current window
driver.quit(); // Quits the browser session and closes all windows
Â
Click Here - Learn Selenium WebDriver
Post a Comment