WebDriver methods in selenium


Once driver object is created, we can perform below methods on a driver.

  1. get(url) : It loads new webpage in the current window.
    Example :
driver.get("http://www.google.com");

more information :

https://onecompiler.com/posts/3sw7b9pmy/webdriver-script-that-opens-browser-and-searches-today-s-weather-in-google-com

  1. navigate() : It allows the driver to access the browsers history. This method returns "WebDriver.Navigation" interface.
    Interface has the following methods:
    • back(): Moves back in the history.
    • forward() : Moves forward in the history.
    • refresh(): Refresh the current page
    • to(url) : Loads a new webpage in the current browser window.
      more information :

https://onecompiler.com/posts/3sw7thdj5/sample-selenium-test-scripts-for-navigating-an-application

  1. quit() : It closes every associated sites
    Example:
 driver.quit(); 
  1. close() : It closes current site,which is open by webdriver class.
    Example :
driver.close(); 
  1. switchTo() : This method helps handling web pop up's, working with frames, go to active element and back to main page or frame.
    Example :
driver.switchTo().alert().accept(); // accepts the popup
driver.switchTo().alert().dismiss(); // dismiss the popup
driver.switchTo().alert().sendkeys(); // Enter the value and accept the popup
  1. getCurrentUrl() : It gets the current url and represents a
    string.
    Example :
 driver.getCurrentUrl();
  1. getPageSource() : It gets source of the page
    Example :
driver.getPageSource();
  1. getTitle() : Its gets the title of the current page and verifies the page title
    Example :
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("Today's weather");
System.out.println(driver.getTitle());// returns the current page title and prints in the console 
  1. manage() : It returns "WebDriver.Options" interface. Interface has the below methods
    • deleteAllcookies();
    • getCookies()
    • timeouts()
    • window()
      Example:
 WebDriver driver = new ChromeDriver();
 driver.manage().window().maximize(); // maximizes the current page
 driver.get("https://www.google.com/"); 
  1. findElements(By by) : It finds all elements within the current page using the given mechanism.
    Example :
 List <WebElement> inputs= driver.findElements(By.xpath("//"));
  1. findElements(By by) : It finds first WebElement using the given mechanism.
    Example :
WebElement search = driver.findElement(By.xpath("//"));