Wednesday, January 15, 2020

XPATH Finding Techniques with Examples


Different Ways to Find XPATH with examples:
 
//input[@type= ‘email’ or @name= ‘email’]

//*[contains (@placeholder, ‘Organization)]

//a[starts-with(text(), 'START')]

//input[starts-with(@name, ‘organization)]

//button[contains(text(),’ Signup’ )]

//div[@class= ’ col-sm-12 google-sign-form’]/input[2]

//ul[@class=’nav navbar-nav navbar-right’]//li[@class=’sign-in’]

//input[@name=’ organization_name’]//following::input[1]

 //li[@class=’sign-in’]//following-sibling::li

//input[@name=’password’]//preceding::input[1]

//li[@class=’login’]//preceding-sibling::li[1]

//div[@class=’ col-sm-12 google-sign-form’]/child::input[1]

//input[@name=’email’]//parent::div

//div[@class=’ col-lg-3 col-md-4 col-sm-6 sign-form’]//descendant:: input

 //input[@name=’email]//ancestor::div[1]

Types Of XPath in Selenium2.1   Absolute
2.2  Relative
Writing Dynamic XPath in Selenium by different ways:
3.1  Basic XPath
3.2  Using ‘OR’ & ‘AND’
3.3  Using Contains()
3.4  Using Starts-With
3.5  Using Text()
3.6  Using Index
3.7  Using Chained XPath
3.8  XPath Axes
3.8.1   Following
3.8.1.1  Following Sibling
3.8.2   Preceding
3.8.2.1  Preceding Sibling
3.8.3  Child
3.8.4  Parent
3.8.5  Descendants
3.8.6  Ancestors

Single Attribute://<HTML tag>[@attribute_name='attribute_value']
or
//*[@attribute_name='attribute_value']

//input[@id='Email']
or
 //*[@id='Email']

Multiple Attribute://<HTML tag>[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]
 or
 //*[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]

//input[@id='Email'][@name='Email']
or
 //*[@id='Email'][@name='Email']

Using AND://<HTML tag>[@attribute_name1='attribute_value1' and @attribute_name2='attribute_value2]
or
//*[@attribute_name1='attribute_value1' and @attribute_name2='attribute_value2]

//input[@id='Email' and @name='Email']
 or
//*[@id='Email' and @name='Email']

 Using OR://<HTML tag>[@attribute_name1='attribute_value1' or @attribute_name2='attribute_value2]
 or
 //*[@attribute_name1='attribute_value1' or @attribute_name2='attribute_value2]

//input[@id='Email' or @name='Email']
 or
 //*[@id='Email' or @name='Email']

contains()://<HTML tag>[contains(@attribute_name,'attribute_value')]
 or
 //*[contains(@attribute_name,'attribute_value')]

//<HTML tag>[contains(@attribute_name,'attribute_value')]
or
//*[contains(@attribute_name,'attribute_value')]
//input[contains(@id,'Email')]

or
 //*[contains(@id,'Email')]
 or
 //input[contains(@name,'Email')]
 or
 //*input[contains(@name,'Email')]

 starts-with()://<HTML tag>[starts-with(@attribute_name,'attribute_value')]
 or
 //*[starts-with(@attribute_name,'attribute_value')]

//input[starts-with(@id,'Ema')]
 or
 //*[starts-with(@id,'Ema')]

 text()://*[text()='New look for sign-in coming soon']

last():findElement(By.xpath("(//input[@type='text'])[last()]"))

[last()-1] – Selects the last but one element (of mentioned type) out of all input element present
findElement(By.xpath("(//input[@type='text'])[last()-1]"))

position():findElement(By.xpath("(//input[@type='text'])[position()=2]"))
 or
 findElement(By.xpath("(//input[@type='text'])[2]"))

Finding elements using index
findElement(By.xpath("//label[2]"))

following: //*[@id='FirstName']

To identify the input field of type text after the FirstName field, we need to use the below xpath.
//*[@id='FirstName']/following::input[@type='text']

To identify just the input field after the FirstName field, we need to use the below xpath.
//*[@id='FirstName']/following::input

preceding://*[@id='LastName']
//*[@id='LastName']//preceding::input[@type='text']"

No comments:

Post a Comment