Sunday, 16 March 2014

How to identify the elements using xpath in Selenium Webdriver in C#.Net

In this article I am going to explain you how we can use xpath to identify the web elements using selenium web driver in C#.Net.

What is xpath in selenium web driver?

xpath is used to find the specific element in the given webpage.
Some of the below examples will demonstrate how we can write the xpath expressions.

Find all elements with tag input
//input
Find all input tag element having  attribute type = ‘hidden’
//input[@type='hidden']
Find all input tag element having  attribute type = ‘hidden’  and name attribute = ‘ren’
//input[@type='hidden'][@name='ren']
Find all input tag element with attribute type containing ‘hid’
//input[contains(@type,'hid')]
Find all input tag element with attribute type starting with ‘hid’
//input[starts-with(@type,'hid')]
Find all elements having innertext = ‘password’
//*[text()='Password']
Find all td elements having innertext = ‘password’
//td[text()='Password']
Find all next siblings of td tag having innertext = ‘gender’
//td[text()='Gender']//following-sibling::*
Find all elements in the 2nd next sibling of td tag having innertext = ‘gender’
//td[text()='Gender']//following-sibling::*[2]//*
Find input elements in the 2nd next sibling of td tag having innertext = ‘gender’
//td[text()='Gender']//following-sibling::*[2]//input
Find the td which contains font element containing the text ‘12’
//td[font[contains(text(),'12')]]
Find all the preceding siblings of the td which contains font element containing the text ‘12’
//td[font[contains(text(),'12')]]//preceding-sibling::*

More xpath Expressions.

Operation
Xpath
Find all elements with tag input
//input
Find all input tag element having  attribute type = ‘hidden’
//input[@type='hidden']
Find all input tag element having  attribute type = ‘hidden’  and name attribute = ‘ren’
//input[@type='hidden'] [@name='ren']
Find all input tag element with attribute type containing ‘hid’
//input[contains(@type,'hid')]
Find all input tag element with attribute type starting with ‘hid’
//input[starts-with(@type,'hid')]
Find all input tag element with attribute type ending with ‘den’
//input[ends-with(@type,'den')]

//input[substring(@value, string-length(@value) - string-length('me') +1) = 'me']
Find all input tag element with attribute value matching the pattern xyz
//input[matches(@value,'nam')]
Find all td tags that has the exact text xyz in it
//td[text()='xyz']
Find all td tags that contains text xyz in it
//td[contains(text(),'xyz')]
Find all tr tags that are ancestor of current td tag
//td/ancestor::tr
Find all tr tags that are descendant of current td tag
//td/descendant::tr
Find all following siblings of td
//td/following-sibling::td
Find all preceding siblings of td
//td/preceding-sibling::td
Find all the children of div
//div[text()='abc']/child::*
Find the parent of div
//div[text()='abc']/parent::*

Below example illustrates how we can use xpath expressions in selenium webdriver in C#.Net.
How to use xpath expressions in selenium web driver in C#.Net?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using OpenQA.Selenium;

using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;
namespace Abc
{
    class Program
    {
        static void Main(string[] args)
        {

           
IWebDriver driver=null;
try
{
driver = new ChromeDriver(@"F:\selenium\csharp");
driver.Url = "http://www.google.co.in";

                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));

driver.Navigate();

//in below statement we have identified Google search box using xpath expression.

driver.FindElement(By.XPath("//*[@id='lst-ib']")).SendKeys("Selenium Book in C#");

            }

 catch(Exception e){
 Console.WriteLine("Exception ******"+e.ToString());
               
 }
           
            finally{
            Thread.Sleep(2000);
            driver.Quit();
            Console.ReadLine();
            }

           
        }
    }
}



What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors