webdriverio icon indicating copy to clipboard operation
webdriverio copied to clipboard

Polyfill for nth-match CSS 4 selector

Open Risto-Stevcev opened this issue 11 years ago • 6 comments

It would be nice to have the nth-match pseudo-selector polyfill when using WebdriverIO.

In jQuery, this can easily be achieved programatically with something like: $('elem.someclass').eq(2), but it would require using messy xPath in the current version of WebdriverIO, because the nth element of .someclass can't be selected using any other CSS selector except for currently unavailable CSS 4 nth-match.

There's the elements protocol, but there's no way to send the nth element from that to another command.

It would be nice to either have that polyfill like this:

browser.selectByValue('select.comparators:nth-match(2)', 'Some value')

Which could be polyfilled by using the this.elements protocol and selecting the WebElement JSON index from what is asked for in nth-match. If there's no nth-match in the selector, it can just default to it's current behavior and use this.element.

Or something generic and similar to jQuery, which would detect if the selector argument is left out and would attempt to read the result from the previous promise, such as this:

browser.elements('select.comparators').eq(2).selectByValue('Some value')

Risto-Stevcev avatar Nov 21 '15 12:11 Risto-Stevcev

Let's also enable :visible and :hidden

christian-bromann avatar Dec 23 '15 03:12 christian-bromann

I'm running into a problem with this right now. Is this issue scoped for any future release?

jkwaldrip avatar May 12 '17 15:05 jkwaldrip

@jkwaldrip this wasn't really high on my prio list. Happy to receive PRs for that. You can work around this now by doing:

const comparators = $$('select.comparators');
comparators[2].selectByValue('Some value');

christian-bromann avatar May 12 '17 18:05 christian-bromann

@christian-bromann Thanks, I've added an update on #1140 for the issue I was encountering with setValue() in case anyone else runs into it.

jkwaldrip avatar May 15 '17 16:05 jkwaldrip

This should probably be closed. nth-match doesn't even exist anymore. It also was never equivalent to jQuery's :eq() selector (which is confusing, which is why it was removed)

Though, getting support for the :eq() selector would be extremely useful..

BlueRaja avatar Aug 28 '18 16:08 BlueRaja

agree with @BlueRaja. My workaround above is actually the valid solution. You can also use .filter to limit the selection by arbitrary functions, e.g.

        const results = $$('.commands.property a').filter(function (link) {
            return link.isVisible();
        });

christian-bromann avatar Aug 28 '18 16:08 christian-bromann