Olivvv
Olivvv

Reputation: 1200

Protractor - select next sibling of the current element

In my code there a list of elements (tr) selected with "protractor.By.repeater". This list is looped using "forEach".

Within this loop, the element is clicked, and this click should trigger the inclusion of a new "tr" just after the clicked element.

I want to select that new line.

I used :

var nextRow = tr.$(protractor.By.xpath('following-sibling::tr'));

but then with :

                nextRow.isDisplayed(function(row){
                    console.log('row', row);
                });

it generates errors such as : " UnknownError: java.util.HashMap cannot be cast to java.lang.String"

Is there another way to achieve what I want, i.e to select the next sibling of the current element ?

Or did I wrote something incorrect there ?

Thanks for any help!

Upvotes: 4

Views: 4870

Answers (2)

user9975398
user9975398

Reputation:

Using Xpath selectors is not a better choice as it slows down the element finding mechanism.

I have designed a plugin to address this specific issues: protractor-css-booster

The plugin provides some handly locators to find out siblings in a better way and most importantly with CSS selector.

using this plugin, you can directly use:

var nextRow = element(by.css('table tr.first')).element(By.followingSibling('tr'));

Hope, it will help you...

Upvotes: 0

Leo Gallucci
Leo Gallucci

Reputation: 16722

Looks like you are missing a .then there and misinterpreted what isDisplayed does.

Code should be more like:

nextRow.isDisplayed().then(function(visible) {
  console.log('is displayed? ', visible);
});

Also your ElementFinder nextRow doesn't look quite alright, what is variable tr below? and Protractor $ is an alias for element(by.css which takes a string argument, not a webdriver.Locator like you interpreted below, in your code:

var nextRow = tr.$(protractor.By.xpath('following-sibling::tr'));

So instead maybe you meant:

var tr = $('table tr.first'); // just guessing here since you didn't provide that code
var nextRow = tr.element(By.xpath('following-sibling::tr'));

Upvotes: 8

Related Questions