1

I am trying to do some unit testing with selenium2 using the following code:

private const string TicketName = "Automated Test Ticket";

[Test]
public void EditTicketTest() {

    var tableData = driver.FindElement(By.XPath("//td[contains(text(), '" + TicketName + "')]"));

}

The test fails with the following reason:

OpenQA.Selenium.NoSuchElementException : Unable to locate element: {"method":"xpath","selector":"//td[contains(text(), 'Automated Test Ticket')]"}

But when I look at the page and inspect the element, the text is definitely inside the tag. Is it possible that there is some excess spacing or something else that could be causing it to not recognize the text?

Here is the HTML:

<tr data-id="55">
    <td>55</td>
    <td class="ticket-title">
    <span data-original-title="Automated Test Ticket" class="work-on-ticket-note-icon-tickets" data-toggle="tooltip" data-placement="right" data-trigger="hover" title=""></span>                          <span data-original-title="This is a work on ticket note for a company!" class="work-on-ticket-note-icon-companies" data-toggle="tooltip" data-placement="right" data-trigger="hover" title=""></span>                         Automated Test Ticket
    </td>
    <td>Medium</td>
    <td>Active</td>
    <td>8/25/2014<br> <small>(0 changes)</small></td>
    <td><a href="#" class="modal-open" data-modal="company-details-modal" data-url="/companies/details/1"></a></td>
    <td>
            <strong class="text-danger">None Assigned</strong>

    </td>
    <td>
        <a href="#" class="modal-open btn btn-sm btn-success" data-modal="edit-modal" data-url="/{controller}/edit/55"><span class="glyphicon glyphicon-edit"></span></a>
        <a href="#" class="modal-open btn btn-sm btn-info" data-modal="log-modal" data-url="/{controller}/log/55"><span class="glyphicon glyphicon-list-alt"></span></a>
        <a href="#" class="modal-open btn btn-sm btn-warning" data-modal="addtime-modal" data-url="/{controller}/addtime/55"><span class="glyphicon glyphicon-time"></span></a>
    </td>
</tr>
1
  • 1
    NoSuchElementException is usually because 1. The element you're trying to locate is in a <frame> or <iframe>, or 2. The element you're trying to locate is not yet displayed on the page when the attempt is made. Commented Aug 25, 2014 at 19:20

1 Answer 1

2

The td in question contains multiple empty text nodes as children, and when text() is used in a function that takes a string, it will evaluate to the string value of the first matching node in document order, so this:

//td[contains(text(), 'Automated Test Ticket')]

Is evaluating to something like this:

//td[contains("            ", 'Automated Test Ticket')]

Which will always produce an empty nodeset.

Two options here are this:

//td[contains(., 'Automated Test Ticket')]

which will match any td that has a contiguous "Automated Test Ticket" anywhere within it, or this:

//td[text()[contains(., 'Automated Test Ticket')]]

which will match any td that has an immediate child text node containing the text "Automated Test Ticket".

I prefer the first option because it's cleaner and has a better chance of turning up a match if you're not completely sure what the td is going to contain.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.