selenium-webdriver Navigation Browser methods in WebDriver


WebDriver, The main interface to use for testing, which represents an idealised web browser. The methods in this class fall into three categories:

  • Control of the browser itself
  • Selection of WebElements
  • Debugging aids

Key methods are get(String), which is used to load a new web page, and the various methods similar to findElement(By), which is used to find WebElements. In this post we are going to learn browser controlling methods. get

void get(java.lang.String url)

Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete. it is best to wait until this timeout is over, since should the underlying page change whilst your test is executing the results of future calls against this interface will be against the freshly loaded page. Usage

//Initialising driver
 WebDriver driver = new FirefoxDriver();
 //setting timeout for page load
 driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
 //Call Url in get method


java.lang.String getCurrentUrl()

Get a string representing the current URL that the browser is looking at. It returns the URL of the page currently loaded in the browser.


//Getting current url loaded in browser & comparing with expected url
 String pageURL = driver.getCurrentUrl();
 Assert.assertEquals(pageURL, "");


java.lang.String getTitle()

It returns the title of the current page, with leading and trailing whitespace stripped, or null if one is not already set.


//Getting current page title loaded in browser & comparing with expected title
 String pageTitle = driver.getTitle();
 Assert.assertEquals(pageTitle, "Google");


java.lang.String getPageSource()

Get the source of the last loaded page. If the page has been modified after loading (for example, by Javascript) there is no guarantee that the returned text is that of the modified page.


//get the current page source
 String pageSource = driver.getPageSource();


void close()

Close the current window, quitting the browser if it's the last window currently open. If there are more than one window opened with that driver instance this method will close the window which is having current focus on it.


//Close the current window


void quit()

Quits this driver, closing every associated window. After calling this method we can not use any other method using same driver instance.


//Quit the current driver session / close all windows associated with driver

These are all very useful methods available in Selenium 2.0 to control browser as required.