Selenium Browser Commands cheat sheet

Selenium Browser Commands cheat sheet

In order to automate any web application it is necessary to open the browser, fetch the title and close the browser after performing few actions. Selenium Webdriver contains commands which enables such browser operations.

By typing driver. syntax we get below list of methods,

Selenium browser commands

Now let us go through the different Browser commands and methods which Selenium uses to interact with an HTML page.

Key Selenium browser Commands

Get Commands

get (String args0) : void – This method is used to load the web application on the active webdriver. The return type is void and accepts String as a parameter.

Syntax – driver.get(“<URL>”);

How to use the above method in script?

String website = “http://techcanvass.com/”;

driver.get(website);

OR

driver.get(“http://techcanvass.com/”);

The above code will load Techcanvass website.

Get Title Command

getTitle() : String – This method is used to fetch the title of the web page. The return type is String and it accepts nothing as parameter.

Syntax – driver.getTitle();

How to use the above method in script?

driver.get(“http://techcanvass.com/”);

String title=driver.getTitle();

System.out.println(“Title “+title);

The above code will fetch the title of Techcanvass website and display on consol.

Get Current URL Command

getCurrentUrl() : String – This method is used to fetch the URL of the web page which webdriver is currently active. The return type is String and it accepts nothing as parameter.

Syntax – driver. getCurrentUrl();

How to use the above method in script?

String CurrentURL = driver.getCurrentUrl();

System.out.println(“Current URL “+CurrentURL);

OR

System.out.println(driver.getCurrentUrl());

Now let us learn how to use getCurrentUrl() method through an example:

driver.get(“http://techcanvass.com/”);

System.out.println(driver.getCurrentUrl());

The above code will fetch the URL of Techcanvass website and display on console.

Get Page Source Command

getPageSource() : String – This method is used to fetch the page source of the web page which webdriver is currently active. The return type is String and it accepts nothing as parameter.

Syntax – driver. getPageSource ();

How to use the above method in script?

String PageSource = driver. getPageSource ();

System.out.println(“Page Source “+ PageSource);

OR

System.out.println(driver. getPageSource());

Now let us learn how to use getPageSource() method through an example:

driver.get(“http://techcanvass.com/”);

System.out.println(driver. getPageSource());

The above code will fetch the page source of Techcanvass website and display on console.

Close Command

close() : void – This method is used to close the active web  browser on which the webdriver is working. The return type of this method is void and it accepts nothing as parameter.

Syntax – driver.close();

Now let us learn how to use close() method through an example:

driver.get(“http://techcanvass.com/”);

driver.close();

The above code will close the Techcanvass window.

Quit Command

quit() : void – This method is used to close all the web  browsers which webdriver had opened. The return type of this method is void and it accepts nothing as parameter.

Syntax – driver.quit();

The above code will close the all the browsers opened by the webdriver.

An Example

Now let us see how different Web Element methods can be used in a single script.

package TechC;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class WebElements {

                public static void main(String args []) throws InterruptedException

                 {            

                                System.setProperty(“webdriver.chrome.driver”, “<Path>”);         

                                WebDriver driver = new ChromeDriver();

                                driver.get(“http://techcanvass.com/”);

                                String title=driver.getTitle();

                                System.out.println(“Title “+title);

                                String Currenturl=driver.getCurrentUrl();

                                System.out.println(“Current Url “+Currenturl);

                                String pagesource=driver.getPageSource();

                                System.out.println(“Page Source “+pagesource);

                                driver.close();

                 }

}

The above code will launch Techcanvass website. It will fetch the title, current URL and page source of the application. Later the webdriver will close the browser. The output should be visible on console.

About Techcanvass

Techcanvass is an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

Techcanvass offers wide range of automation testing courses. These courses include Java and Selenium certification course, Python Selenium Certification course, BDD Training with Cucumber, RPA Training with UiPath, DevOps certification course and more automation testing courses.

Selenium Certification Training