Selenium WebDriver Cheetsheet

In the previous tutorial, we looked at only two basic actions that can be performed using Selenium WebDriver. However, Selenium WebDriver supports a wide range of interactions and actions to automate web applications effectively.

Here is a brief cheat sheet covering commonly used interactions and actions in Selenium WebDriver.

Element Interactions

// Type text
driver.findElement(By.id("username")).sendKeys("testuser"); 

// Clear input field
driver.findElement(By.id("password")).clear();    

// Click button           
driver.findElement(By.id("loginBtn")).click();  

// Check if checkbox is selected             
driver.findElement(By.id("checkbox")).isSelected();  

// Check if radio is enabled        
driver.findElement(By.id("radio")).isEnabled();              

// Check if radio is enabled
driver.findElement(By.id("logo")).getText();  


Dropdown Handling

Select dropdown = new Select(driver.findElement(By.id("dropdownId")));
dropdown.selectByVisibleText("Option");
dropdown.selectByIndex(2);
dropdown.selectByValue("value");


Mouse Actions

Actions actions = new Actions(driver);
// Hover over element
actions.moveToElement(element).perform();    
// Double-click              
actions.doubleClick(element).perform(); 
// Right-click                   
actions.contextClick(element).perform();      
// Drag and drop             
actions.clickAndHold(element).moveToElement(target).release().perform(); 


Frame Handling

// Switching to frame
driver.switchTo().frame("frameName"); 
// Back to main page   
driver.switchTo().defaultContent(); 


Multiple Window Handling

//Switch to New Window
// Store the current (parent) window handle
String parentHandle = driver.getWindowHandle(); 
// Loop through all open window handles
for (String winHandle : driver.getWindowHandles()) {
    if (!winHandle.equals(parentHandle)) {
        // Switch to the new window
        driver.switchTo().window(winHandle);
        // Print the URL of the new window
        System.out.println("New window URL: " + driver.getCurrentUrl());
        // Close the newly opened window
        driver.close();
    }
}
// Switch back to the parent window
driver.switchTo().window(parentHandle);
// Print the URL of the parent window
System.out.println("Parent window URL: " + driver.getCurrentUrl());


Alert Handling

// Handling alerts
Alert alert = driver.switchTo().alert();
// Click OK
alert.accept();  
// Click Cancel    
alert.dismiss();   
 
// Read alert message  
alert.getText();   


Page Handling

// Opens the specified URL in the browser.
driver.get("URL")
// Returns the title of the current web page.
driver.getTitle()
// Returns the current page URL.
driver.getCurrentUrl() 
// Returns the page's HTML source code.
driver.getPageSource()
// Returns the ID of the current browser window.
driver.getWindowHandle()
// Returns the IDs of all open browser windows.
driver.getWindowHandles()
// Maximizes the browser window.
driver.manage().window().maximize() 
// Minimizes the browser window.
driver.manage().window().minimize()
// Makes the browser window full screen.
driver.manage().window().fullscreen()
// Sets the implicit wait time for finding elements.
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10))
// Navigates to a URL (same as driver.get()).
driver.navigate().to("URL")
// Navigates back to the previous page.
driver.navigate().back()
// Moves forward to the next page.
driver.navigate().forward()
// Refreshes the current page.
driver.navigate().refresh() 
// Closes the current browser tab/window.
driver.close()
// Closes all browser windows and ends the WebDriver session.
driver.quit()
// Finds a single web element by ID.
driver.findElement(By.id("id"))

 

Related Tutorials

Related Questions






Read more