Selenium WebDriver Methods with examples

Selenium WebDriver Methods with examples,You will learn WebDriver methods with real time examples ,WebDriver methods are going to use when you find the elements in a Web Pages.We are going to use Selenium WebDriver Methods only when you gather all required element objects in a Application Under Test(AUT) for test automation.Let's see Selenium WebDriver Methods one by one with real time examples.
Selenium WebDriver Methods with examples


Must Read

Generate HTML Reports using Selenium
Read Data From properties file using Selenium
Launch Firefox Browser using GeckoDriver
Selenium-Testng examples to execute multiple classes
Selenium Tutorials

Selenium WebDriver Methods with examples

All Selenium WebDriver Methods will be available when you type driver. and click on Control+Shift keys in keyboard ,you will find all available WebDriver methods as below.

get()
getTitle()
getPageSource()
getWindowHandles()
getWindowHandle()
getCurrentUrl()
findElement(By arg0)
findElements(By arg0)
manage()
navigate()
close()
quit()

Now let's see all above Selenium WebDriver Methods with examples.

WebDriver Method : get(String ar0)

WebDriver get method is useful in navigate to particular web page in the current window browser and it will get the web page with the help of HTTP protocol.
Systax : driver.get(String arg0)

Example:driver.get("http://www.learn-jmeter.blogspot.in")

@Test

 public void verifyTitle(){



driver = new FirefoxDriver();

 driver.get("http://www.learn-jmeter.blogspot.in");

 }

WebDriver Method : getTitle()

Get Title selenium method is useful in capture or get the webpages page titles to assert whether particular webpage is loaded correctly or not in your test scripts.
Syntax : driver.getTitle();

Example:

@Test

 public void verifyTitle(){



 driver = new FirefoxDriver();

 driver.get("http://www.learn-jmeter.blogspot.in");

 String Pagetitle =driver.getTitle();

 System.out.println(Pagetitle);

 }


WebDriver Method : getPageSource()

WebDriver getPageSource will useful in get particular webpage source code means when you click on right click on webpage you will see option view pagesource,this method also works same and it will print in Eclipse Console or any other tool which ever you are using.
Syntax : driver.getPageSource();

Example:

@Test

 public void verifyTitle(){



 driver = new FirefoxDriver();

 driver.get("http://www.learn-jmeter.blogspot.in");

 String page_source = driver.getPageSource();

 System.out.println(page_source);

 }

WebDriver Method : getWindowHandles()

WebDriver Method getWindowHandles is useful in identifying the all open multiple windows in a webpage or return the windows which are open in webpages to switch into particular window to work with test scripts as per your requirements to identify the elements.
Syntax:driver.getWindowHandles();

Example:

@Test

 public void verifyTitle(){



 driver = new FirefoxDriver();

 driver.get("https://www.naukri.com");

 String Pagetitle =driver.getTitle();

 System.out.println(Pagetitle);

 Set<String> windowslist= driver.getWindowHandle();

 System.out.println("Another Window-"+windowslist);



 }

Output:

Jobs - Recruitment - Job Search - Employment - Job Vacancies - Naukri.com
Another Window-[{c4599435-555f-4586-9710-1d11ecfa71fd}, {3c66555d-be51-4f4e-ae90-076a63161b1e}]
PASSED: verifyTitle

WebDriver Method : getWindowHandle()

This method return only one window in a webpage to identify the elements or to switch into particular open unique window to work in a webpages.
Syntax : driver.getWindowHandle();
Example:

@Test

 public void verifyTitle(){



 driver = new FirefoxDriver();

 driver.get("https://www.naukri.com");

 String Pagetitle =driver.getTitle();

 System.out.println(Pagetitle);

 String windowslist= driver.getWindowHandle();

 System.out.println("Another Window-"+windowslist);



 }


Output:

Jobs - Recruitment - Job Search - Employment - Job Vacancies - Naukri.com
Another Window-{af476171-896a-4ad6-b96f-c6fa8d4c586e}
PASSED: verifyTitle

As you above seen in gerWindowHandles() method giving two windows name but in getWindowHandle() method giving only one window.

WebDriver Method : getCurrentUrl()

getCurrentUrl method will return current open window browser current URL in any browser like Firefox , Chrome or IE browsers.
Syntax : driver.getCurrentUrl()

Example:

@Test

 public void verifyTitle(){



 driver = new FirefoxDriver();

 driver.get("http://learn-jmeter.blogspot.in");

 String url_current = driver.getCurrentUrl();

 System.out.println("Current open url is -"+url_current);



 }


Output:

Current open url is -http://learn-jmeter.blogspot.in/
PASSED: verifyTitle

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

WebDriver Method : findElement(By arg0)

findElement is useful in identifying the first element in a webpage whether it may be text box,list,dropdown etc.Parameter is By which is a identifying elements like key.
Syntax : driver.findElement(By aeg0)

Example:

@Test

 public void verifyTitle(){



 driver = new FirefoxDriver();

 driver.get("http://learn-jmeter.blogspot.in");

 String url_current = driver.getCurrentUrl();

 System.out.println("Current open url is -"+url_current);

 String pagename = driver.findElement(By.xpath("/html/body/div[4]/div[2]/div[2]/
div[2]/div[1]/div[2]/div[2]/div[1]/div/div/ul/li[2]/a")).getText();

 System.out.println("Particular menu name is :"+pagename);



 }

Output:

Current open url is -http://learn-jmeter.blogspot.in/ ,
Particular menu name is :Jmeter Tutorial
PASSED: verifyTitle

WebDriver Method : findElements()

findElements Webdriver method find all elements within the current open webpage,let's see below example so that you will understand clearly.Ini below example i have used to get all available elements by tagname as "tr".

List element = webtables.findElements(By.tagName("tr"));
Syntax : driver.findElements(arg0)

Example:

@Test

 public void verifyTitle(){

 driver = new FirefoxDriver();

 driver.get("http://indiagoldrate.com/gold-rate-in-hyderabad-today.htm");

 WebElement webtables=driver.findElement(By.xpath("/html/body/div[4]/div[2]/table[1]"));
 if (webtables.isDisplayed()){

 System.out.println("Webtable is displayed");

 }else{

 System.out.println("Webtable is not displayed.");

 }

 //Now save the rows in a list

 List element = webtables.findElements(By.tagName("tr"));

 int Countrowsno = element.size();

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

 System.out.println("Number of Rows count in a webtable is :"+Countrowsno); 

 }

Output:

Webtable is displayed
Number of Rows count in a webtable is : 14
PASSED: verifyTitle
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

WebDriver Method : Manage()

This webdriver method is useful in performing different options like Timeouts,IMplicitWait,MAximize the windows etc commands etc you can insert in your test scripts.
Syntax:driver.manage().window().maximize();

Example:

@Test

 public void open_Browser(){

 driver = new FirefoxDriver();

 driver.get("http://learn-jmeter.blogspot.in");

 driver.manage().window().maximize();

 }

Output:

PASSED: open_Browser
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

WebDriver Method : navigate()

Webdriver navigate method to open any webpage and navigating to forward and back options like moving or redirecting from one page to other pages as below.
Syntax : driver.navigate()

Example:

@Test

 public void open_Browser(){

 driver = new FirefoxDriver();

 driver.navigate().to("http://learn-jmeter.blogspot.in");

 driver.navigate().forward();

 driver.navigate().back();

 }

WebDriver Method : close()

This method useful in closing the current open window browser in Selenium WebDriver.
Syntax:driver.close()

Example:

@Test

 public void open_Browser(){

 driver = new FirefoxDriver();

 driver.get("http://learn-jmeter.blogspot.in");

 driver.manage().window().maximize();

 driver.navigate().to("http://learn-jmeter.blogspot.in");

 driver.navigate().forward();

 driver.navigate().back();

 }

 @AfterMethod

 public void close_Browser(){

 driver.close();

 }

WebDriver Method : quit()

Quit method closes all open current window browsers in a system which is open by Selenium Webdriver test scripts.
Syntax: driver.quit();

Example:

@Test

 public void open_Browser(){

 driver = new FirefoxDriver();

 driver.get("http://learn-jmeter.blogspot.in");

 driver.manage().window().maximize();

 driver.navigate().to("http://learn-jmeter.blogspot.in");

 driver.navigate().forward();

 driver.navigate().back();

 }

 @AfterMethod

 public void close_Browser(){

 driver.quit();

 }


I hope you got enough knowledge on Selenium Webdriver methods with real time examples,please share this post in case you like it and provide your valuable comments on this post.

3 comments:

  1. The blog gave idea about Selenium Web Driver and Methods. The syntax and methods shared was very much useful and helpful my sincere thanks for sharing this post
    Selenium Training in Chennai

    ReplyDelete
  2. Thanks for taking time to share this post.It is really useful.Continue sharing more like this.
    Regards,
    Selenium Training in Chennai

    ReplyDelete
  3. Very nice article this would definitely help the beginners, coding made easy with the help of example you shared. Selenium Training in Chennai

    ReplyDelete