Web Automation Using Selenium
  • About The Book
  • Selenium
    • Introduction
      • What is testing and why?
      • What is Selenium and Why?
    • Getting Started
      • Audience
      • Prerequisite
      • Set up
      • Getting Started - Hello World..!!
      • What are drivers
    • Locators
      • CSS Selectors
      • XPath Selector
    • WebDriver
      • WebDriver Methods
      • WebDriver Types
    • WebElement(s)
      • WebElement Methods
      • Looping Through WebElements
    • Waits In Selenium
  • TestNG
    • Introduction
    • TestNG Setup
    • First Test Script
    • Some of the features of TestNG
    • TestNG Annotations
      • @Test
      • @DataProvider
  • Maven
    • Introduction
  • Framework Development
    • Introduction
    • Building Framework
      • Technology Stack
      • Page Object Model (POM)
      • Setup
      • What our framework contain
      • Packages
        • Context
        • Factory
        • Listeners
        • Pages
      • Creating first automation test
  • Common Interview Questions
    • Selenium-Related
Powered by GitBook
On this page
  • WebDriver methods explained:
  • get()
  • getCurrentUrl()
  • getTitle()
  • getPageSource()
  • navigate()
  • close()
  • quit()
  • findElement(By by)
  • findElements(By by)

Was this helpful?

  1. Selenium
  2. WebDriver

WebDriver Methods

Methods inside WebDriver

PreviousWebDriverNextWebDriver Types

Last updated 3 years ago

Was this helpful?

WebDriver methods explained:

Now we know WebDriver object is a java object representation of the browser, we can use methods inside it and interact with our web browser.

get()

  1. This is the method that we user to open a URL in the browser.

  2. This method accept a string parameter which will be the URL the we want to open

  3. URL should be well structured (meaning with the protocol either http:// or https://)

Example:

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

getCurrentUrl()

  1. This method return the current URL that is open in the browser as String.

Example:

String currentUrl = driver.getCurrentUrl();

getTitle()

  1. This method returns the title of the current page that is opened in the browser.

  2. This will be the value of <title> tag inside the <head> section of the current page source.

String title = driver.getTitle();

getPageSource()

  1. This method with return the whole html of the page as String.

String source = driver.getPageSource();

navigate()

  1. This method helps navigating in the browser based on it's history. Like forward, back, refresh, navigating to some URL etc.,)

driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
driver.navigate().to("https://www.facebook.com/");

close()

  1. This method closes the main window but it'll not close other windows.

driver.close();

quit()

  1. This method closes all open windows.

driver.quit();

findElement(By by)

  1. This method finds the particular element in the page which matches the given locator mechanism mention inBy .

  2. If the specified locator mechanism is matching more than one element then Selenium will always selects the first one to appear in the page source.

WebElement element = driver.findElement(By.name("q"));
WebElement element = driver.findElement(By.id("submit-button"));

findElements(By by)

  1. This method finds the list of all elements in the page which matches the given locator mechanism mention inBy .

List<WebElement> elements = driver.findElements(By.className("q"));
List<WebElement> elements = driver.findElement(By.tagName("a"));

By is a class in selenium library which has static methods with all the locator mechanisms.

Example: By.id, By.name, By.cssSelector etc.,

Example:

Example:

Example:

Example:

Example:

To know more about locators

here
here
here
here
here
here
Here
Locators