Skip to main content

XPath Expression to locate Web Element

In last article i mentioned you how to locate the Web Element with id, Name, LinkText, Partial LinkText, CSS Selectors, Class Name and Tag Name. We use FireBug add-one to use these 7 locators to locate the Web Element on Web Page. Now we are talking about XPath expression. Which is also a type of Locator. We need FirePath add-ons to use it.

XPath can be identify in 2 ways

1. Absolute XPath: Absolute XPath is identify and generated from the root node. In Absolute XPath a prefix "/" is added.

/html/body/div/div/div/div/form/fieldset/div/input[@id='email']

let me show you a snapshot how to get it
Selenium Web Driver, XPath , Locators, Absolute Xpath, Automation Testing

2. Relative XPath: In Relative Xpath the node will be from where you want to start and it start with the "//". In Relative Xpath we do not need to start from the root node.

Below is the image for which i will write down the various method to write Relative XPath
Relative XPath, Automation Testing, Selenium Web Driver, Locators

Below is the code that showing in above screen shot
<div id="gaia_firstform" class="form-panel first valid">
<div class="slide-out ">
<div class="input-wrapper focused">
<div id="identifier-shown"><div>
<label class="hidden-label" for="Email">   Enter your email</label>
<input id="Email" type="email" autofocus="" spellcheck="false" value="" placeholder="Enter your email" name="Email"/>
<input id="Passwd-hidden" class="hidden" type="password" spellcheck="false"/>
</div>
</div>
<span id="errormsg_0_Email" class="error-msg" role="alert"/>
</div>


a. When you click on the FirePath always you have a XPath. For this a XPath is showing

.//*[@id='Email']

b. By using tag name and attribute 

//input[@id='Email']
//input[@type='email']
//input[@name='Email']
//input[@placeholder='Enter your email']

c. to locate the element by using  XPath where some text is visible on the web page

See the below image to locate Web page
Absolute XPath, Automation Testing, Locators, Relative XPath, Selenium Web Driver, XPath Locators

 To locate this "Create account" web element following will be the XPath. By using contains keyword user need to put some text of whole line that need to locate. 

//a[contains(text(),'Create account')]
d.To locate the link by using contains keyword below is the code



//a[contains(@href,'https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail&hl=en')]

Comments

Popular posts from this blog

What is API?

In general way, API (Application Programming Interface) is the intermediate software which is useful for two applications to communicate. API is the tool which is used for interaction of two applications. If developer developing any software which have multiple blocks (Modules) than developer use APIs to put them together.

Ad-Hoc Testing

Ad-Hoc Testing is the testing type which is performed without any proper procedure and without any Requirement Specification. Ad-Hoc testing is totally informal testing which have intension to break the application as early as possible and found important defect early.  In Ad-hoc testing tester do not have any test cases so only the users who have very high knowledge regarding the application only they can perform Ad-Hoc Testing.