XPath for a span based on its text?

32,527

Solution 1

If you want to select span with text - Edit Studen try any of this:

//span[@title='Edit Student']/span
//span[text()='Edit Student']

If you want to select Edit Studen with role="button" try any of this:

//span[@title='Edit Student'][@role='button']
//span[@role='button'][./span[text()='Edit Student']]
//span[@role='button'][./span[.='Edit Student']]

Solution 2

This XPath,

//span[@role='button' and normalize-space() = 'Edit School']

will select the span elements with a @role of button and a normalized string value of Edit School. You can, of course, further qualify its heritage as necessary.

See also Testing text() nodes vs string values in XPath

Solution 3

simply use can use any of this xpaths

//span[contains(text(),'Edit Student')]
//*[contains(text(),'Edit Student')]
//span [@class='button-grid-action kendo-lexia-tooltip icon-pencil']/span
//span [@title='Edit Student']/span
//span [contains(@title,'Edit Student')]/span
//span [contains(@class,'button-grid-action kendo-lexia-tooltip icon-pencil')]/span

Solution 4

This should get the span text

//span[.='Edit Student']
Share:
32,527
Rock
Author by

Rock

Day: As usual boring automation engineer with very less knowledge of java , java script Night: Thinking of learning something new but cant For fun : Want to be blogger, learning how to start blogging. lets see if this is my life changing move :) PeaceNLove

Updated on September 28, 2020

Comments

  • Rock
    Rock over 3 years

    I am unable to locate first span with XPath I tried:

    //*[@id='student-grid']/div[2]/div[1]/table/tbody/tr[1]/td/span/span[contains(text(), 'Edit School')]
    

    to select span with text - Edit Student button

    <tbody role="rowgroup">
    <tr class="" data-uid="2f3646c6-213a-4e91-99f9-0fbaa5f7755d" role="row" aria-selected="false">
    <td class="select-row" role="gridcell">
    <td class="font-md" role="gridcell">marker, Lion</td>
    <td role="gridcell">TESTLINK_1_ArchScenario</td>
    <td role="gridcell">1st</td>
    <td role="gridcell">Not Started</td>
    <td role="gridcell"/>
    <td role="gridcell"/>
    <td role="gridcell">QA Automation TestLink Folders</td>
    <td class="k-cell-action" role="gridcell"/>
    <td class="k-cell-action detail-view-link font-md" role="gridcell">
    <span class="button-grid-action kendo-lexia-tooltip icon-pencil" role="button" title="Edit Student">
    <span>Edit Student</span>
    </span>
    </td>
    <td class="k-cell-action archive-link font-md" role="gridcell">
    <span class="button-grid-action kendo-lexia-tooltip icon-archive" role="button" title="Archive Student">
    <span>Archive Student</span>
    </span>
    </td>
    </tr>
    </tbody>