set method

Future<Online> set(
  1. AbstractSelector selector,
  2. Object? obj, {
  3. Object? max = null,
  4. bool show = true,
  5. Duration? timeout = null,
  6. int index = 0,
  7. bool click = false,
})

Implementation

Future<Online> set(AbstractSelector selector, Object? obj,
    {Object? max = null,
    bool show = true,
    Duration? timeout = null,
    int index = 0,
    bool click = false}) async {
  String text = obj?.toString() ?? '';
  if (show) Show.set_value(selector.selector, text);

// first wait for the page to load and the selector to appear
  if (selector is XPath) {
    await (await page).waitForXPath(selector.selector);
  } else {
    await (await page).waitForSelector(selector.selector);
  }
  var selectedElements = await (await page).$$(selector.selector);

  if (selectedElements.length > index) {
    var selected = selectedElements[index];

    if (click) {
      await selected.click();
    }

    await selected.evaluateHandle('''(element, text) => {
    if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) {
      element.focus(); // Focus on the element
      var inputEvent = new Event('input', {bubbles: true});
      var changeEvent = new Event('change', {bubbles: true});
      element.dispatchEvent(inputEvent);
      element.value = text;
      element.dispatchEvent(changeEvent);
      element.blur(); // Remove focus from the element
    }
  }''', args: [text]);
  }

  return this;
}