Using XPath to identify Web objects from Selenium WebDriver

来源:互联网 发布:淘宝晒单福利图 编辑:程序博客网 时间:2024/04/28 07:02

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 withname as deposit, XPath can be formed as XPath("//Input[@name='deposit']") andC# 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 withclass as navtab_selected, XPath can be formed as XPath("//td[@class='navtab_selected']") andC# 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 withtype as button and name as btnGo, XPath can be formed as XPath("//input[@type='button' and @name='btnGo']") andC# 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']
      0 0
      原创粉丝点击