Selenium Webdriver Commands Methods For Freshers & Experience

Selenium Webdriver Commands Methods For Freshers & Experienced: When we think about automation and whether the requirement is an open-source tool with all functionalities. Then, one name comes to our mind: Selenium WebDriver. Because Selenium WebDiver is one of the open-source tools that support all major features of Selenium Webdriver Commands Methods, we can test any web application. Also, it can be used with different scripting languages like JAVA, PYTHON, C#, and others. But Java is used with selenium widely because of its popularity, and both have a common thing: both are open-source tools.

So, in this Selenium Tutorial post, we want to share with you the commonly used Selenium Webdriver Commands Methods, which are mostly used by any automation testers with the syntax and a simple example that helps you understand the basics easily.

Let’s briefly and precisely discuss all commonly used Selenium Webdriver command methods one by one so that you can use those Selenium Webdriver command methods effectively whenever the requirement comes.

One of the common statements familiar to you is the below statement. To understand why we wrote this statement, you can visit our previous post, Selenium WebDiver Hierarchy, and Selenium WebDiver Architecture post.

WebDriver driver= new firefoxDriver ();

Selenium Webdriver Commands Methods

Top Selenium Webdriver Commands Methods
Top Selenium Webdriver Commands Methods

get(): When you are required to open a URL in the web browser, you can use this method of selenium WebDiver.

package selenium.basics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_close 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      driver.get("https://www.google.com");
      driver.close();
   }
}

Navigate.To(URL): We can load a new web page in the current browser window using this method. When this method executes internally, an HTTP get operation is performed, and during that time period, it will block all other operations until the web page is loaded completely.

driver.navigate.To(URL);
package selenium.basics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_NavigateTo 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      driver.navigate().to("https://www.google.com");
      driver.close();
   }
}

getCurrentUrl(): When you want to get the currently open web page URL, you can use this method, which returns the current opened page URL in the string format.

String openedURL= driver.getCurrentUrl(); 
System.out.println("Opened URL is:"+openedURL);
package selenium.basics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_getCurrentUrl 
{
   public static void main(String[] args) throws InterruptedException 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      driver.navigate().to("https://www.google.com");
      Thread.sleep(2000);
      String Cururl=driver.getCurrentUrl();
      System.out.println("The CUrrent URL is: "+Cururl);
      driver.close();
   }
}

getPageSource(): By using the getPageSource() method, you can get the page source of the opened web page, and this method returns in string format.

String openedPagePageSource= driver.getPageSource(); 
System.out.println("Page source is: \n" +openedPagePageSource );
package selenium.basics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_getPageSource 
{
   public static void main(String[] args) throws InterruptedException 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      driver.navigate().to("https://www.google.com");
      Thread.sleep(2000);
      String PGSource=driver.getPageSource();
      System.out.println("Page Source Is: "+PGSource);
      System.out.println("Complete Page Source Is Printed");
      driver.close();
   }
}

getTitle(): You can able to get the current web page <title> tag texts or web page title of the opened web page by using the getTitle() method. getTitle() returns the page title in the form of a string.

String pageTitle= driver.getTitle(); 
System.out.println("Loaded page title: "+pageTitle);
package selenium.basics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_getTitle 
{
   public static void main(String[] args) throws InterruptedException 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      driver.navigate().to("https://www.google.com");
      Thread.sleep(2000);
      String PGtitle=driver.getTitle();
      System.out.println("Page Source Is: "+PGtitle);
      driver.close();
   }
}

Close(): After performing all the operations in the current window, we want to close that. So, we can close the active window by calling the close() of selenium WebDiver.

driver.close();
package selenium.basics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_close 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      driver.get("https://www.google.com");
      driver.close();
   }
}

quit(): in the previous syntax, we can see that by using the close() method, we can close the currently active window, but suppose there are 5 windows opened, and you want to close all opened windows; that time, you can use the quit() method, and it will close all the opened windows irrespective if active or non-active.

driver.quit();
package selenium.basics;

import java.util.Set;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_quit 
{
   public static void main(String[] args) throws InterruptedException 
   {
      
      WebDriver driver;
      
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      driver=new FirefoxDriver();
      driver.get("https://www.google.com");
      Thread.sleep(1000);
      driver=new FirefoxDriver();
      driver.navigate().to("https://www.google.com/intl/en-GB/gmail/about/#");
      
      Set<String> size=driver.getWindowHandles();
      System.out.println("Total Windows : "+size.size());
      
      driver.quit();
   }
}

findElement(locator): If you want to find an element in the active dom, you can use findElement() with the locators.

WebElement SoftwareTestingoLogo= driver.findElement(By.xpath("//img[@title='SoftwareTestingo']")); 
System.out.println(SoftwareTestingoLogo);
package selenium.findElement;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_WebDriver_FindElement 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();

      driver.navigate().to("https://www.google.com/");
      driver.findElement(By.xpath("//a[contains(text(),'Gmail')]")).click();
      driver.close();
      

   }
}

findElements(locator): Suppose there are multiple similar types of elements, and you want to access those elements, then you can use the findElements() method. it will return a list of all those similar elements in insertion order.

List allLinks= driver.findElements(By.xpath("//a")); 
System.out.println("Total elements found: "+allLinks.size());
package selenium.findElement;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_WebDriver_FindElements 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();

      driver.navigate().to("https://www.google.com/");
      List<WebElement> li= driver.findElements(By.tagName("a"));
      System.out.println("Total Number Of Links:  "+li.size());
      driver.close();
      

   }
}

fullScreen():  This method will open the application in full screen.

driver.manage().window().fullscreen();
package selenium.basics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_fullScreen 
{
   public static void main(String[] args) throws InterruptedException 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      WebDriver driver=new FirefoxDriver();
      driver.get("https://www.google.com");
      driver.manage().window().fullscreen();
      Thread.sleep(10000);
      driver.close();
   }
}

maximize(): This method will maximize the browser.

driver.manage().window().maximize();
package selenium.basics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_Maximize 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      driver.manage().window().maximize();
      driver.get("https://www.google.com");
      driver.close();
   }
}

getPosition(): By using this, we can know the position of the window from the left side in terms of X and Y coordinates. the return type of this method is Point (An instance of the Point class).

Point position= driver.manage().window().getPosition(); 
System.out.println("Position of window is: "+position);
package selenium.basics;

import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_getPosition 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      Point position= driver.manage().window().getPosition(); 
      System.out.println("Position of window is: "+position);
      driver.get("https://www.google.com");
      driver.close();
   }
}

getX() and getY(): These methods help find elements with specific X and Y coordinates on the window. The getX() will return the position from the left side of the window, and getY() will return the position from the top side of the screen.

driver.manage().window().getPosition().getX(); 
driver.manage().window().getPosition().getY()
package selenium.basics;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_getX_YCoordinates 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      int x=driver.manage().window().getPosition().getX();
      int y=driver.manage().window().getPosition().getY();
      System.out.println("The X & Y Cordinates is: "+ "( "+x+", "+y+")");
      driver.get("https://www.google.com");
      driver.close();
   }
}

getSize(): We can get the window size by implementing this method.

Dimension size= driver.manage().window().getSize(); 
System.out.println("Size of current window:"+size);
package selenium.basics;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_getSize 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      Dimension size=driver.manage().window().getSize();
      System.out.println("Size of the Current Window: "+size);
      driver.get("https://www.google.com");
      driver.close();
   }
}

getWidth() and getHeight(): By using these two getWidth() and getHeight() methods, we can get the window height and width of the current window.

System.out.println("Width of window: "+ driver.manage().window().getSize().getWidth()); 
System.out.println("Height of window"+driver.manage().window().getSize().getHeight());
package selenium.basics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_getwidth_height 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      System.out.println("Width of window: "+ driver.manage().window().getSize().getWidth()); 
      System.out.println("Height of window: "+driver.manage().window().getSize().getHeight());
      driver.get("https://www.google.com");
      driver.close();
   }
}

setPosition(): With the help of this method, we can able to set the position of the window on the screen.  within the setPosition() method, you must pass the x and Y coordinates.

driver.manage().window().setPosition(new Point(50,200));
package selenium.basics;

import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_setPosition 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      driver.manage().window().setPosition(new Point(50,200));
      driver.get("https://www.google.com");
      driver.close();
   }
}

setSize(): we are using this method to set the window size in software testing. The first parameter represents the width, and the second represents the height. you can find the syntax below

driver.manage().window().setSize(new Dimension(300,500));
package selenium.basics;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_setSize 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      driver.manage().window().setSize(new Dimension(300,500));
      driver.get("https://www.google.com");
      driver.close();
   }
}

deleteAllCookies(): Sometimes, while testing an application, you may face some issues because of the cookies. so we can solve issues by calling the deleteAllCookies() method in our program to delete all the cookies of the specific domain, and after that, we can run our program.

driver.manage().deleteAllCookies();
package selenium.basics;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Webdriver_Methods_deleteCookies 
{
   public static void main(String[] args) 
   {
      String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe";
      //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe";

      System.setProperty("webdriver.gecko.driver", FirefoxDriverPath);

      // To Disable the Driver Logs & Store that logs into logs.txt
      System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
      System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt");

      WebDriver driver=new FirefoxDriver();
      driver.manage().deleteAllCookies();
      System.out.println("Cookies Deleted");
      driver.get("https://www.google.com");
      driver.close();
   }
}

deleteCookieNamed(): we can also delete some specific cookies we want to delete by using the deleteCookieNamed() method, and in that method, we have to pass the cookie name we want to delete. like this, we can delete the specific cookies as per our requirement.

driver.manage().deleteCookieNamed("Some cookie name");

implicitWait(): Sometimes, some elements require extra time to load, but our script is not waiting to load at that time. so we can stop the script execution for some time using the implicitWait() method. We have written a detailed post about Selenium. Wait, you can check here.

Timeouts implicitWaitEx= driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

pageLoadTimeout(): Sometimes, because of the server issues, the page load times increase compared to the normal page load time. Because of that, we get errors for such scenarios when using pageLoadTimeout() with the wait time.

driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

setScriptTimeout(): Using the setScriptTimeout() method, we can mention the wait time duration for an asynchronous script to finish the execution. If it is not executed within the mentioned time duration, then it will go through an error.

driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

Back (): if you have opened a web URL using navigate.to() method, you want to return to the previous URL. Then, by using this method, we can navigate back to the previous page. We have written a detailed post about the Selenium Navigation command.

driver.navigate().back();

Forward (): Similarly, if you want to go to the next page, then you can use this forward() method along with navigate(). We have written a detailed post about the Selenium Navigation command.

driver.navigate().forward();

Refresh (): With this method’s help, we can refresh the page. We have written a detailed post about the Selenium Navigation command.

driver.navigate().refresh();

switchTo(): at the time of testing an application, if you get any alerts, then you can use this method

driver.switchTo().alert();

defaultContent(): Suppose you are inside a frame and want to come out from the frame; then you can use the defaultContent() method.

driver.switchTo().defaultContent();

Frame (int index): Suppose multiple frames are present, and you want to jump to a specific frame; then, by using the frame(int index), you can move to the particular frame.

driver.switchTo().frame(2);

Frame (String name/id): If you know the frame name or ID, you can switch from one frame to another by using the name or ID of that frame.

driver.switchTo().frame("name or ID");

Frame (WebElement element): Suppose you don’t know the name, ID, or index of a frame, but you can get the locator details of the frame. also, you can switch from your current frame to that frame by this:

driver.switchTo().frame(" WebElement locator");

parentFrame(): Suppose you are present inside an inner frame, and from that inner frame, you want to switchTo the parentFrame; then that time, you can use this method.

driver.switchTo().parentFrame();

getClass(): This method helps you to find out which browser class or browser you are using

Class className= driver.getClass(); 
System.out.println(className.getSimpleName());

isEnabled(): With this isEnabled(), you can easily find out that a specific element in the DOM is enabled or disabled. If the particular element is enabled, then it returns a true boolean value, and if that specific element is disabled, then it returns a false value.

boolean textBox = driver.findElement(By.xpath("//input[@name='textbox1']")).isEnabled();

Click(): if you want to perform some operation, like clicking on an element, you can use the click() method.

driver.findElement(By.name("ok")).click();

submit(): submit is an alternative of click(). When you are finding an element in DOM, and you find a button with an attribute type=submit, then, in that case, you can use submit. In some scenarios where the action is done through elements rather than a button in that place, submit works, and click won’t.

driver.findElement(By.xpath("//input[@name='comments']")).submit();

Select (): We mostly see a drop-down in the DOM. To deal with the drop-down scenarios, we are using the select class. we can select a value by using any of these 3 methods: selectByVisibleText(), selectByValue(), or selectByIndex().

WebElement mySelectedElement = driver.findElement(By.id("select")); 
Select dropdown= new Select(mySelectedElement); 
dropdown.selectByVisibleText("Apple");

getScreenshotAs(): During the execution of the script in some places, you may see that your script is failing, and knowing the reason for failing, you need to take a screenshot of the page. so we can take the screenshot by using the getScreenshotAs() method.

File shot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
FileUtils.copyFile(shot, new File("D:\\ shot1.jpg"));

Still, there are so many methods that you are using during your automation, but I try my best to share a few regular methods, and I hope this post may help you. If you find that there are still other regularly used methods, then you can comment in the comment section, and I try to update those methods in the list.

Thanks for visiting our post. It’s an appeal to you guys. Kindly share your experience with us by using our Contact Us page so that we can make this platform more helpful.

Searching Keyword: Top Selenium Webdriver Commands Methods, Advanced Selenium Webdriver Commands Methods, Selenium Webdriver Commands Methods important for an interview purpose.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment