Selenium Locators usage

There are 8 Locators
          1.Id
          2.Name
          3.ClassName
          4.TagName 
          5.LinkText
          6.PartialLinkText
          7.CssSelector
          8.Xpath


Usages of the  Locators   Webdriver driver=new chromedriver();
driver.FindElement(By.id("id"));

driver.FindElement(By.name("name"));

driver.FindElement(By.ClassName("classname"));
    These class names and tagnames mostly used for likely elements if see above example when we enter class name then we will get all the text fields present in that page we can enter all the fields by using loops.



driver.FindElement(By.TagName("tagname"));
  These class names and tagnames mostly used for likely elements if see above example when we enter class name then we will get all the elements present in that page we can use them by using loops.


driver.FindElement(By.LinkText("linktext"));

driver.FindElement(By.PartialLinkText("partiallinktext"));
 we have the above example to know about the partial linktext.


7.CssSelector:
   
  • Tag and ID  :  driver.findElement(By.cssSelector("input#email")).sendKeys("tftffu");
  • Tag and class :driver.findElement(By.cssSelector("input.inputtext")).sendKeys("tftffu");
  • Tag and attribute : driver.find;Element(By.cssSelector("input[id='email']")).sendKeys("tftffu");
  • Tag, class, and attribute :driver.findElement(By.cssSelector("input.inputtext[id='email']")).sendKeys("tftffu");
  • Inner text : 


8 . xpath
  • Absolute xpath : it starts from html tag node onwards example see below picture

html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td/input

This is an absolute xpath which is very faster than relative xpth because it starts from html node and it didnt find any thing and it is very dangerous to use in our project because when any element or any label is added between them it will fail.
  • Relativexpath : this is a path we directly found it from attributes and tagnames and classnames and preceeding classes or following classes

                     

                     
                      1.parent :::::  //tagname[@attribute='value']/parent::* 
                                   in the above example i will write code as 
                                          //label[@name='ravi']/parent::*
                      2.child  :::::::://tagname[@attribute='value']/child::*
                                          for example in "tr" attribute is class='parent'
                                          //tr[@class='parent']/child::*
                      3.following sibiling:::::  //tagname[@attribute='value']/following-sibling::tagname 
                                                          //tr[@class='parent']/following-sibling::td
following sibling means same parent and different childs like 

                             div
                              |
           ---------------------------------   
         |                   |                    |
         |                   |                    |
        tr                  td                 div

tr and td and div have one parent namely div 
                      4.precceding sibiling
in the dom structure when we take an element  above of the elemnt is preceding and below is the following 

                      5.ansector :::::   //tagname[@attribute='value']/ansector::tagname
                                                //input[@name='email']/ancestor::table



the difference between ancestor and parent is it doesnt bother about the relations it directly find the tagname which you have mentioned above the code
                      6.descendant :::: //tagname[@attribute='value']/descendant::tagname
                                                //input[@name='email']/descendant::table
the difference between ancestor and child is it doesnt bother about the relations it directly find the tagname which you have mentioned below the code
                      7.position
                        for example we have a 10 'td' is in the webpage but we need the 3 then directly we can mention number   td[3] 

                      8.starts with
   //label[starts-with(@id,'message')]
                      9.ends with
  //label[ends-with(@id,'message')]
                     10.contains
//*[contains(@name,'btn')]
                      11. and  or in xpaths
    //*[@type='submit' or @name='btnReset']
//input[@type='submit' and @name='btnLogin']



     when ever you open the webpage please press rightclick and press inspect then you will find dom structure and then you can press ctrl+f to find locators weather they are correct or not
























Comments

Popular posts from this blog

Java Theory interview questions

java programmes frequently asked in interview