Create Selection from Element

Using Selection.each(fn), the callback is passed an index and an Element. Element has a different API than Selection, and within the callback I’d like to use the Selection API on the passed Element.

In jQuery, I’d often do this:

$('li').each(function (index, element) {
  let container = $(element).closest('div.listContainer');

I’ve tried inside the callback to do things like $(element) or Selection(element) but it errors saying those are undefined. (Kind of stabbing in the dark, since I don’t see in the docs how to do this.)

Is there a way in the jQuery-like Selection API in k6 to do this?

HI @mithlond, welcome to the forum!

It seems like there is no way to get the Selection out of an Element :frowning: I have created an issue.

My current workaround will be to just not use each but instead use a while loop and next(). Which unforuntately also has an issue.

So my current workarond code will be something like:

import http from "k6/http";

export default () => {
    let li = http.get("").html().find("li");
    for (; li.size() > 0; li = {
        let ul = li.closest('ul.header-icons');

You might need to check closest response as well and check it’s size() to be certain something was matched.

Hope this helps you and you are welcome to give some inside in the issues I linked