Friday, 13 March 2015

PRACTICE FOR XPATH

Locators:

1.ID
2.name
3.Partiallinktext
4.LinkText
5.classname
6.CSSselector
7.Xpath



Using ID:

copy html code for tat particular element ...paste in Notepad...


<input id="username" class="textField" type="text" value="" name="username" 
   placeholder="Username">



Step 1:

driver. findelement(By.id("username")).sendkeys("admin");

Step 2:using xpath

//tagname [@property name='propertyvalue']


tagname= input

property name= id

property value = username


Xpath: //input[@id='username']



NOTE:

If  id  value is number present  dont take id....plz remeber...dont take id...
 eg..

<input id="gs_taif0" class="gsfi" disabled="" autocomplete="off" aria-hidden="true" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; position: absolute; z-index: 1; background-color: transparent; color: silver; left: 0px;" dir="ltr">


tagname= input

property name= id

property value= gs_taif0  -- here 0 is number  so dont take id..bcz it wil change every refresh..



Using NAME:

<input id="username" class="textField" type="text" value="" name="username" 
   placeholder="Username">

Step 1:

driver. findelement(By.name("username")).sendkeys("admin");

Step 2:using xpath

synatax:

//tagname [@property name='propertyvalue']


tagname= input

property name= name

property value = username


Xpath: //input[@name='username']



Note:

If id is has Number in property value  than give prepference to name......       




Using Text:

<label id="keepLoggedInLabel" for="keepLoggedInCheckBox" 
   style="-moz-user-select: none; cursor: default;">Keep me logged in</label>


tagname= label

property name= text

property value = Keep me logged in



syntax

//tagname[ property name()= 'property value']

//label[text()='Keep me logged in']



Note:

for text mostly use  "contains" bcz sme gap wil be there.....if space is thr r not use  "contains"

actually tats is gud..to find text.....

Syntax

//tagname[ contains( property name,'property value')]

//label[contains(text(),'Keep me logged in')]




CSS selector-  Cascaded  Style sheet

syntax

htmltag [Property name='property value']



<input id="username" class="textField" type="text" value="" name="username" 
   placeholder="Username">


htmltag = input

property name= name

property value = username



CSS:   input[name='username']

            input[type='text']   or   in firepath u can see  css option is thr u can use tat one...

  

Using xpath:

This is gud way to find element.....


<input type="submit" jsaction="sf.chk" name="btnK" aria-label="Google Search" value="Google Search">


syntax

//tagname[@property name='Property value']


tagname= input


property name= value


property value = Google



1.//input[@value='Google']


2.//input[@name='Google']


3.//input[@type='Google']


4.//input[@style='Google']


5.//input[@id='Google']



6.//input[text()='Google']

7.//input[contains(text,'Google')]


8.
//input[@value='Google']/../../...     tis new action nwdys using to go parent element


Relative xpath:

eg;

<table style="width:100%">
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>



//td[text()='Eve']

parent element

//tr[td[text()='Eve']]


new method nowdys using tis way....


//td[text()='Eve']/..                 tis go to tr parent element



//td[text()='Eve']/../tr[2] 



Using Gruop Index

check in html  code if hve same id nd same name nd same value nd same classname...

thn go for group index...




(//div)[2]

(//div)[3]

To find last element in group index we use ...(las t ) word... first word not possible...

(//div)[last]


1 comment:

  1. Hi Arun,

    Am new to selenium, your all posts helped me to learn selenium.Can you please post a keyword driven example in selenium with the source code.

    Thank you

    ReplyDelete