Thursday, September 12, 2019

Selenium WebDriver Methods And Locators

Hi, As we all learned the Introduction to selenium WebDriver and Architecture of selenium WebDriver in previous blogs. Now we mainly concentrate on different types of selenium web driver methods and Locators.

Following are different selenium WebDriver methods used to perform the different actions on the web page.

  1. get().
  2. getCurrentUrl().
  3. findElement().
  4. findElements().
  5. getTitle().
  6. close().
  7. quit().
  8. naviagte().
  9. manage().
  10. swithTo().
  11. click().
  12. clear().

Now, We learn each method syntax and which action can be performed using these methods.

1. Method Name:- get().
    Syntax:- get(URL).
    Example:- driver.get("https://easylearn29.blogspot.com").
    Purpose:- This method used to load the web page in the browser window, this will be done using HTTP get method.
    Parameter:- Parameter should be a fully qualified URL.

2. Method Name:- getCurrentUrl().
    Syntax:- getCurrentUrl().
    Example:- driver.getCurrentUrl().
    Purpose:- This method used to fetch the current URL of the web page displayed in the browser window and the returned data will be in String data type.
    
3. Method Name:- getTitle().
    Syntax:- getTitle().
    Example:- driver.getTitle().
    Purpose:- This method used to fetch or retrieve the Title of the web page displayed in the browser window and the returned data will be in String data type.

4. Method Name:- findElements().
    Syntax:- findElements(By by).
    Example:- driver.findElements(By.xpath("//div[1]\div[0]").
    Purpose:- This method used to find all the web elements in the currently displayed web page and this method will return an Array of WebElement.
    Parameter:- We use different types of the locator to find the element, we will discuss the Locator section briefly in the later part of the blog.
    
5. Method Name:- findElement().
    Syntax:-WebElement findElement(By by).
    Example:- driver.findElement(By.xpath("//div[1]\div[0]").
    Purpose:- This method used to find the web element in the currently displayed web page and this method will return a data of  WebElement data type. 
    Parameter:- We use different types of locator to find the element.
    
NOTE: 
  • This will only fetch the first element it has matched the locator we have specified. Even though there are multiple elements matches.

6. Method Name:- close().
    Syntax:- void close().
    Example:- driver.close().
    Purpose:- This method used to close the current window displayed in the browser.

NOTE: 
  • If the multiple windows are opened in the browser, It will close only the one window which is currently displayed.  
  • If the single window is opened in the browser, It will act as quit() method.

7. Method Name:- quit().
    Syntax:- void quit().
    Example:- driver.quit().
    Purpose:- This method used to close all the window opened in the browser and close the browser. 
Interview Question:
1. What is the difference between close () and quit() method?
2. Give me anyone real-time example when we use quit() over close() and vice-versa?
8. Method Name:- navigate().
    Syntax:- Webdriver.Navigation naviagte().
    Example:- driver.navigate.to(URL).
    Purpose:- This method used to move in between the module of the web page.

9. Method Name:- manage().
    Syntax:- Webdriver.options manage().
    Example:- driver.manage().
    Purpose:- This method provides a set of the optional interface, Which will have sub-method to perform action window, browser, and a Web page which we will discuss in later chapters.

10. Method Name:- switchTo().
      Syntax:- swicthTo().     
      Example:- driver.switchTo(URL).
      Purpose:- This method used to switch between the frames of the window, it mostly used when we need to handle the Dialog box, Popup, Upload file window, etc

11. Method Name:- click().
      Syntax:-  WebElement.click().
      Purpose:- This method used to right-click on the web element which displayed in the window of the browser.

12. Method Name:- clear().
      Syntax:-  WebElement.clear().
      Purpose:- This method used to clear the data entered in the web element of the web page like text boxes.


All these methods are helpful to perform an action on webElement that are displayed on the web page.

To before using these method we need to find those web element for web page, we can find the web element using findElement() method, which needs locators to find the element. 


What are Locators?

             Locators are the commands which inform the selenium IDE on which web element the particular action should be performed.


Following are different types of locators used in Selenium. 

  1. Id.
  2. Name.
  3. Tag Name.
  4. Link Text.
  5. Partial Link Text.
  6. Xpath.


1. Id:-

             Id locator is most preferred and used locator to find the web element. 
             Id's are always unique for each web element in the selected web page.
             It is fast and safe among all locators.

 Syntax to use Id locator in findElement().

                  WebElement testElement = driver.findElement(By.id ("testId"));

We need to replace the testId by real Id of the element, Id for the element can be found by inspecting the web page.






2. Name:-

                  Name Locator is the name of the webElement
                  It is used when Id of the element not found and present
                  We can not rely on this locator more because some times Web page can have multiple elements which have same name.
                   In the above case, if we use the Name locator to find the element, the method will find the first element which matches the locator value.

Syntax to use Name locator in findElement().

                  WebElement testElement = driver.findElement(By.name("testName"));

We need to replace the testName by real name of the element, Name for the element can be found by inspecting the web page.







3. Tag Name:- 
                 
                       In this locator, we use the Tag name to find the element.
                       We can't rely on the Tag name as it also used multiple-element in web page.
                       As we all know there are different tags in the HTML and each element in web page should have any one of the tag
                        
Syntax to use Tag Name locator in findElement().

                  WebElement testElement = driver.findElement(By.tagName("testTagName"));

We need to replace the testTagName by real tag name of the element like "label", Tag name for the element can be found by inspecting the web page.


  




4. Link Text:- 

                         This locater can be used to identify the elements if it is a hyper link.
                         It works only for an element has a hyperlink  Ex: Social media links displayed at the bottom of every webpage.

Syntax to use Link Text locator in findElement().

                  WebElement testElement = driver.findElement(By.linkText("testLinkText"));

We need to replace the testLinkText with a real link text value.


5. Partial Link Text:-
               
          This is same as Link text locater but in this case, we can give link text value partial 
          It will find an element which has link text value mentioned in find element method
                          
Syntax to use Partial Link Text locator in findElement().

WebElement testElement = driver.findElement(By.partialLinkText("testPartialLinkText"));

We need to replace the testPartialLinkText with a real Partial link text value.



6. Xpath:-

                 Xpath Locator is least preferred and very complex locater among all locaters
                 To use this locator we need to drive the traverse path for the element in the web.                       It is very difficult to for a web page like Amazon, Facebook, etc
                 Xpath Locator is slow among all locators.
                 To find the XPath for the element we use the Plugins  "Fire Bug" and "Fire Path" and also in chrome, we have copy XPath option to copy XPath of the inspected element.
                           
Syntax to use Xpath locator in findElement().

                  WebElement testElement = driver.findElement(By.xPath("testXpath"));

We need to replace the testXpath with a real Xpath value.


   NOTE:  

  • We can give both Absolute and Relative path as Xpath.



In the next blog, We will learn the difference between Absolute and Relative path also Learn Handling Text box Web elements