Frequently Used Selenium WebDriver Methods (2023)

 

selenium webdriver,selenium,selenium webdriver tutorial,selenium with java,selenium tutorial,selenium tutorials,selenium videos,selenium with python,selenium webdriver with java,selenium webdriver methods,selenium java,selenium tutorial for beginners,selenium methods,learn selenium,selenium testing,selenium training,methods in selenium webdriver,selenium webdriver get methods,webelement methods in selenium,selenium xpath,gettitle method in selenium, selenium,selenium training,selenium tutorial,selenium webdriver,selenium tutorial for beginners,learn selenium,selenium ide,selenium automation,selenium testing,what is selenium,css cheat sheet,selenium webdriver tutorial,selenium framework,selenium python tutorial,python selenium tutorial,intellipaat selenium,xpath expressions cheat sheet,selenium python,python selenium,selenium web scraping,selenium course,selenium scripts


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

Post a Comment (0)

Previous Post Next Post