Browser Commands with Selenium 🌐✨

Β·

3 min read

So, you've just initialized your browser with Selenium, and now you're ready to make it dance to your commands! Let's dive into the exciting world of browser commands that will empower you to open, close, retrieve page sources, and more. πŸš€

get(String args) - Opening New Horizons 🌍

driver.get("https://www.google.com");

The above Selenium command launches a new browser window and navigates to the specified URL. The command accepts a single string type parameter, which is usually the URL of the application under test.

getCurrentUrl() - Where Am I? 🧭

String currentUrl = driver.getCurrentUrl();

As the name suggests, this Selenium command gives us the URL of the page currently loaded in the browser.

getTitle() - The Page's Nameplate πŸ“œ

String pageTitle = driver.getTitle();

If you want to retrieve the title of the currently opened web page, you can use the above command.

getPageSource() - Source of Truth πŸ•΅οΈβ€β™‚οΈ

String pageSource = driver.getPageSource();
boolean containsText = pageSource.contains("String to find");

The above command will help you to get the source of the last loaded page and also to verify if a particular content is present or not by using the contains method.

getClass() - Classy Affairs πŸ’ƒ

public class JavaObjectgetClassExample2{
    public static void main(String[] args) 
    { 
        Object obj = new String("Facebook"); 
        Class a = obj.getClass(); 
        System.out.println("Class of Object obj is : " + a.getName()); 
    } 
}

// OUTPUT
// Class of Object obj is : java.lang.String

If you’re looking to return the run time class name of the object, you can use the above command to get it done.

navigate().to() - A New Beginning πŸ”„

driver.navigate().to("http://google.com");

It creates a new browser window with a new web page. It takes a String parameter and returns a void value.

refresh() - Time Travel πŸ”„β™»οΈ

driver.navigate().refresh();

If you want to test by seeing how the page reacts to being refreshed, you can use the above command to refresh the current window.

back() - Rewind, Please! βͺ

driver.navigate().back();

By using this command you can go back to the previous page that you visited.

forward() - Fast-Forward to the Future ⏩

driver.navigate().forward();

Similar to back, the forward action is also a widely used navigation action. So you can use the above command to redirect to the page you were in before clicking on the back button.

close() - Window's Last Bow πŸšͺ

driver.close();

The close() command is used to close the currently open WebDriver-controlled browser window. If the current window is the only active window in WebDriver, the browser will also be closed.

quit() - Exit Strategy πŸš€βœŒοΈ

driver.quit();

There is a minor distinction between the quit() and close() methods. The quit() method terminates all open browser instances, whereas the close() method terminates only the current browser instance.


With these powerful commands, you're now equipped to navigate the web seamlessly with Selenium. Go ahead, automate your browser interactions, and enjoy the thrill of being the maestro of your digital symphony! πŸŽ‰πŸŽ»

Β