Selenium-WebDriver: Browser Methods

Automating web browsers is made possible through Selenium WebDriver. In this blog, we will explore different browser methods provided by the WebDriver class which is actually an interface in Selenium, for more information refer to this link. Selenium WebDriver offers several methods to use and run automation scripts, In which browser methods are used to perform some operations on the browser. Let's see what are the most commonly used browser methods while writing a Selenium automation script.

  • get() method
  • getCurrentUrl() method
  • getTitle() method
  • getPageSource() method
  • close() method
  • quit() method

In the below demo examples, we used selenium java codes to explore the WebDriver browser methods. To access these methods, we had to call the WebDriver interface class in selenium code by creating its class object. In the first step, we need to set the property for the browser for which we intend to automate and then specify the path or location of the driver file of a particular browser present on the device (to set browser path to refer to this link).

Let's understand how to set browser property and invoke the WebDriver methods.

Syntax:

System.setProperty("webdriver.chrome.driver", “path of browser driver file from device”);

In the above snippet, webdriver.chrome.driver is  Property name and  path of browser driver file from device is value .
Then, by creating the WebDriver object and importing the driver class packages, we need to call the WebDriver class, as shown below code.

WebDriver driver = new ChromeDriver();

Example:

public class BrowserMethods {
	//main method syntax.
    
	public static void main(String[] args) {
    	// Set Property with driver path.
        
		System.setProperty("webdriver.chrome.driver", "/Users/xxx/Documents/Drivers/chromedriver");
        
        //Initialisation fo driver
		WebDriver driver = new ChromeDriver();
       
       // From here we can use driver. object to invoke different browser methods. 
        }
       }

Now we are able to use the browser method in the current class. Using the driver object of the WebDriver class, we can invoke methods present inside it in the program.

get() method:

This method is used to open URLs in new browser windows.
Syntax:
void get(String URL);
here, get() represents method name, void represents return type (means no return value), String url = String Input.

Example:

driver.get("https://plgworks.com/");

In the demo example, we have taken the help of the PLGWorks website. we have created BrowserMethods class, in the main method we have set system property and call driver methods by creating its class object. driver is an object, using object we call get method and have string value as ("https://plgworks.com").

getCurrentUrl()  method:

This method will use to get the current URL that the browser is looking at.
Syntax:
String driver.getCurrentUrl();
here, getCurrentUrl() represent method name , String represent return type.

Code Snippet:

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

//output printing the current URL of the browser.
https://plgworks.com/

getTitle() method:

This method is used to get the title of the web page we are visiting or opening in the browser.
Syntax:
String driver.getTitle();
here, getTitle() represents method name. String represents return type.
Code Snippet:

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

//output printing the Page Tile of the URL ("https://plgworks.com").
PLG Works | Web and Mobile App Product Studio

getPageSource() method:

The getPageSurce() method returns the source code of the currently opened web page.
Syntax:
String getPageSource();
here, getPageSource() represents method name, String represents return type.

Code Snippet:

String pageSource =driver.getPageSource();

System.out.println(pageSource);
// or
System.out.println(driver.getPageSource());

//Output:
// It prints the source code of the currently opened web page.

close() method:

This method is used to close an open tab in the browser. When we use this command in code it will close/terminate a current open URL tab in the browser automatically.
Syntax:
void close();
In syntax, close() represents method name, void represents return type.

Code Snippet:

driver.close();

quit() method:

quit() method is also used for closing tabs but the difference is it will close or quit all tabs in the browser which open through Selenium script or WebDriver. Simply we can say it will close all tabs and exit from the browser automatically.
Syntax:
void quit();
In syntax, quit() represents method name, void represents return type.

Code Snippet:

driver.quit();

One simple fact is that there is no need to use close() and quit() in a single program as it's used for the same purpose, we can use it as per requirement.
Stay tuned for the upcoming blogs.

Ganesh Chavan

Ganesh Chavan

QA Test Engineer at PLGWorks.com