• Articles
  • Tutorials
  • Interview Questions

Selenium Cheat Sheet

Tutorial Playlist

Selenium User Handbook

Are you searching for a handy resource that you can refer to whenever you need to review the fundamental operations of Selenium? Well if you are then this is just what you need.
We at Intellipaat understand that it could be a bit of a hassle to remember every aspect of technology and that is why we have come up with a Selenium cheat sheet to assist our learners whenever they need a handy reference containing all the basics of selenium.

We assume that you have a basic understanding of selenium if you are referring to this cheat sheet.

Download the printable PDF of Selenium cheat sheet

Selenium Cheat Sheet

What is selenium?

Selenium is an open-source framework to automate and perform software testing such as smoke tests, integration tests, etc. on web applications. It provides a playback/recording tool and domain-specific language.

Join our Selenium Training Course and get yourself certified in Selenium!

Get started with Selenium:

  • Driver Initialization Basics:

    • Firefox WebDriver driver = new FirefoxDriver();
    • Chrome WebDriver driver = new ChromeDriver();                 
    • Internet Explorer WebDriver driver = new SafariDriver();
    • Safari Driver WebDriver driver = new InternetExplorerDriver();

Read more about Selenium WebDriver.

  • Driver Initialization Advanced:

A. Load Firefox from a different location:

System.setProperty("webdriver.firefox.bin", "path/to/firefox/binary");
FirefoxProfile fp = new FirefoxProfile();

B. Load Firefox addons

File file = new File("path/to/extension.xpi");
fp.addExtension(file);

Selenium Locators:

Selenium locators are used to find and match the webpage elements that selenium interacts with. Following are some locators in selenium:

  • Locating by ID
driver.findElement(By.id("q")).sendKeys("Se lenium 3");
  • Locating by Name
driver.findElement(By.name("q")).sendKeys ("Selenium 3");
  • Locating by Xpath
driver.findElement(By.xpath("//input[@id='q'])).sendKeys("Selenium 3");
  • Locating Hyperlinksby Link Text
driver.FindElement(By.LinkText("edit this page")).Click();
  • Locating by DOM
dom =document.getElementById('signinForm')
  • Locating by CSS
driver.FindElement(By.CssSelector("#rightbar> .menu >li:nth-of-type(2) > h4"));

Also, check out the blog on CSS Selector in Selenium.

  • Locating by ClassName
driver.findElement(By.className("profileheader"));
  • Locating by TagName
driver.findElement(By.tagName("select")).C lick();
  • Locating by LinkText
driver.findElement(By.linkText("NextP age")).click();
  • Locating by PartialLinkText
driver.findElement(By.partialLinkText(" NextP")).click();

Get 100% Hike!

Master Most in Demand Skills Now !

Selenium Navigators:

The navigator interface in selenium helps in moving backward and forwards in the browser’s history. Following are some navigator commands you can use:

  • Navigate to URL
driver.get(“http://newexample.com”)
driver.navigate().to(“http://newexample.com”)
  • Refresh page
driver.navigate().refresh()
  • Navigate forwards in browser history
driver.navigate().forward()
  • Navigate backward in browser history
driver.navigate().back()

Get ready for a Software Testing job by going through these Top Software Testing Interview Questions and Answers!

TestNG:

TestNG is an open-source framework for automated testing. The NG in TestNG stands for Next Generation. It is similar to Junit but with more functionality to offer. Following are the TestNG annotations:

  • @test: This annotation marks a class or method as a part of a test
  • @BeforeSuite: This annotation makes sure that the method only run once before all the tests have run
  • @AfterSuite: This annotation makes sure that the method runs once after the execution of all the tests
  • @BeforeTest: This annotation will make sure that the method marked with this annotation runs before the first method annotated with @test
  • @AfterTest: This annotation will make sure that the method marked with this annotation runs after all the methods annotated with @test execute all the classes inside <test> tag in the testng.xml file.
  • @BeforeGroups : A method annotated with this annotation will run before all the first test methods run in that specific group
  • @AfterGroups: A method annotated with this annotation will run after all the test methods run in that specific group
  • @BeforeClass: A method annotated with this annotation will run only once per class and before all the first test methods run
  • @AfterClass: A method annotated with this annotation will run only once per class and after all the test methods run
  • @BeforeMethod: A method annotated with this annotation will run before every @test annotated method
  • @AfterMethod: A method annotated with this annotation will run after every @test annotated method

Also, check out the blog on Window Handling in Selenium.

JUNIT

JUNIT (Java Unit Testing Tool) is a framework used to perform unit-level testing. Following are the JUNIT annotations:

  • @Test: test method to run with public void return type
  • @After: method to run after the test method
  • @AfterClass: method to run before the test method
  • @Before: method to run before the test method
  • @BeforeClass: method to run once before any of the test methods in the class have been executed
  • @Ignore: This annotation is used to ignore a method

Go through these most frequently asked JUnit interview questions and prepare yourself for the Software Developer interviews.

Windows:

Sometimes the web applications may have multiple frames or windows. Selenium assigns each window a unique alphanumeric id which is called window handle. Selenium then uses the id to switch control among windows.

String handle=driver.getWindowHandle();
Set<String> handles = getWindowHandles();
driver.switchTo().window(handle);
  • How to switch to a newly created window

String curWindow=driver.getWindowHandle();
  • Get all window handles
Set<String> handles = getWindowHandles();
For(string handle: handles){
If (!handle.equals(curWindow))
{driver.switchTo().window(handle);
}
}

Also, learn how to use Selenium with Java.

Frames

  • Using Frame Index:
driver.switchTO().frame(1);
  • Using Name of Frame:
driver.switchTo().frame(“name”)
  • Using web Element Object:
driver.switchTO().frame(element);
  • Get back to the main document:
driver.switchTO().defaultContent();

Curious to know the Top Manual Testing Interview Questions and Answers for 2023?

Operations:

In selenium there are certain operations that can be performed on the web elements. Following is the list of those operations along with their respective syntax:

  • Launch Webpage:
get("www.webdriverinselenium.com");
  • Click Button: 
findElement(By.id("submit")).click();
  • Handle Alert:
AlertAlertpopup = driver.switchTo().alert();
  • Disable a Field:
getElementsByName('') [0].setAttribute('disabled', '')
  • Enable a Field :
getElementsByName('') [0].removeAttribute('disabled');
  • Screenshot : 
File snapshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(snapshot, new File("C:\screenshot.jpg"));
  • Print the Title of the Page:
String pagetitle = driver.getTitle();
System.out.print(pagetitle);
  • Implicit Wait: 
manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  • Explicit Wait:
WebDriverWait wait = new WebDriverWait(driver, 20);
  • Sleep :
Thread.Sleep(10);

Want to learn about Selenium with Python! Check out our blog on Selenium Python Tutorial for beginners.

Watch this Selenium Projects For Beginners Tutorial

Alerts:

Sometimes a message box pops up on the screen to display some kind of notification to the user or maybe an ask for permission or to display a warning etc. These messages are called alerts. Alert interface provides some ways to handle the alerts in selenium:

Capture the alert message:

driver.switchTO().alert.getText();

Click on the ‘OK’ button of the alert:

driver.switchTO().alert.accept();

Click on the ‘Cancel’ button of the alert:

driver.switchTO().alert.dismiss();

Send some data to the alert box:

driver.switchTO().alert.sendKeys(“Text”);

Also, check out Appium tutorial to learn more about its concepts.

Selenium Grid:

Selenium Grid helps selenium run multiple tests across different operating systems, browsers, and machines in parallel.

  • Start hub:
java-jar selenium-server- standalone-x.xx.x.jar-role hub
  • Start node:
java-jar selenium-server- standalone-x.xx.x.jar-role node-hub
http://localhost:4444/grid/register
  • Server:
http://localhost:4444/grid/console

Become a Test Architect

Read moreRPA Cheat Sheet

Download a Printable PDF of this Cheat Sheet

This would be all for the Selenium cheat sheet. In case you are looking to learn Selenium in-depth then you should definitely check out the Selenium training provided by Intellipaat. In this online training, you will get to learn the automation testing framework for web applications, TDD, selenium architecture, JaCoCo, TestNG, and Sikuli. You will work on real-life projects and assignments and prepare yourself for Certified Selenium Professional certification in this Selenium training. On top of that, you will have 24*7 technical support from our experts here at Intellipaat. Now you will learn more about the Features of Selenium in the next blog.

Want to become a Test Architect, checkout Selenium Automation Testing Interview Questions and ace your interviews. 

Course Schedule

Name Date Details
Selenium Course 20 Apr 2024(Sat-Sun) Weekend Batch
View Details
Selenium Course 27 Apr 2024(Sat-Sun) Weekend Batch
View Details
Selenium Course 04 May 2024(Sat-Sun) Weekend Batch
View Details

Software-Testing-Bootcamp.png