Using XPath to identify Web objects from Selenium WebDriver

Using XPath to identify Web objects from Selenium WebDriver

XPath can be used by following methods,
1. To find a web object by name attribute
driver.FindElement( By.XPath( "//Tag[@name='name']"))

In the below example, to find a web object of <input> tag category with name as deposit, XPath can be formed as XPath( "//Input[@name='deposit']") and C# statement for the same can be formed as
driver.FindElement( By.XPath( "//Input[@name='deposit']"))

Screen reference 

HTML reference
<tr>
<td width="30%" nowrap="" align="left">
<input type="text class=" onkeyup="javascript:doCheckTBox(this,2,2)" size="8" value="2000000"   name="deposit" flat"="" style="text-align: right;">
 </td>
 </tr> 

2. To find a web object by class attribute

driver.FindElement( By.XPath( "//Tag[@class='class-name']"))

In the below example, to find a web object of <td> tag category with class as navtab_selected, XPath can be formed as XPath( "//td[@class='navtab_selected']") and C# statement for the same can be formed as
driver.FindElement( By.XPath( "//td[@class='navtab_selected']"))

Screen reference
HTML reference
<td align="center" class="navtab_selected">
<a href="/">Home</a>
</td> 

3. To find a web object by multiple attributes

driver.FindElement( By.XPath( "//Tag[@type='type-name' and @name='name']"))

In the below example, to find a web object of <input> tag category with type as button and name as btnGo, XPath can be formed as XPath( "//input[@type='button' and @name='btnGo']") and C# statement for the same can be formed as
driver.FindElement( By.XPath( "//input[@type='button' and @name='btnGo']" ))

Screen reference
HTML reference
<input type="button" onclick="javascript: doSubmitCompareRateProduct()" value="Go" class="flat" name="btnGo">

4. To find a web object by using the contained text


I have a well formed  XHTML page. I want to find the web object with URL, for which I have the text that is linked.
Example
<a href="http://TestURL.com">Text of the URL</a>
The  XPath expression to find the URL link with text as  Text of the URL  is
//a[text()='Text of the URL']

你可能感兴趣的:(Using XPath to identify Web objects from Selenium WebDriver)