Browser Commands with Selenium πβ¨
Table of contents
- get(String args) - Opening New Horizons π
- getCurrentUrl() - Where Am I? π§
- getTitle() - The Page's Nameplate π
- getPageSource() - Source of Truth π΅οΈββοΈ
- getClass() - Classy Affairs π
- navigate().to() - A New Beginning π
- refresh() - Time Travel πβ»οΈ
- back() - Rewind, Please! βͺ
- forward() - Fast-Forward to the Future β©
- close() - Window's Last Bow πͺ
- quit() - Exit Strategy πβοΈ
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! ππ»