Hervé Tchikladzé Hervé Tchikladzé - 1 year ago 104
Javascript Question

Protractor - find element with "neighbour"

I'm currently creating tests with protractor and phantomjs.

What I'm trying to do is pretty simple, here is my html code containing the element I want to click.

<tr class="moo" role="foo"
<td class="bar"
<input class="toto" value="42" type="checkbox"></input>
<td class="titi"
<a href="/path/to/something">Test</a>

Here I need to click the checkbox element, quite easy actually with :

But the problem is that there is many checkbox elements and the only difference between them is the "a" line with the text (Test in this example) that changes.

My question is : how can I specify that I want to click the checkbox Where the text in "a" line is "Test" ?

Answer Source

Use a CSS selector to find the row and then the cell:

element(by.cssContainingText('tr[role="foo"]', 'Test'))

Or with an XPath to find the cell with a single call:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download