babalae/better-genshin-impact · error · ArgumentException

{paramName} 必须是集合或 JS Array

Error message

{paramName} 必须是集合或 JS Array

What it means

ParseCollection<T> is the bridge that turns a loosely-typed object (often a ClearScript JS array) into a strongly-typed List<T>. It throws ArgumentException when values is not IEnumerable, or is a string (which is IEnumerable<char> and must be excluded). This fires when the caller hands in a scalar, a JS object, or a plain string instead of an array/list.

Source

Thrown at BetterGenshinImpact/Core/BgiVision/BvPage.cs:160

    }


    /// <summary>
    /// 1080P 分辨率下点击坐标
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    public void Click(double x, double y)
    {
        GameCaptureRegion.GameRegion1080PPosClick(x, y);
    }

    internal static List<T> ParseCollection<T>(object values, string paramName)
    {
        ArgumentNullException.ThrowIfNull(values, paramName);
        if (values is string || values is not IEnumerable enumerable)
        {
            throw new ArgumentException($"{paramName} 必须是集合或 JS Array", paramName);
        }

        List<T> result = [];
        foreach (var value in enumerable)
        {
            if (value is not T typedValue)
            {
                throw new ArgumentException($"{paramName} 中的每个元素都必须是 {typeof(T).Name}", paramName);
            }

            result.Add(typedValue);
        }

        if (result.Count == 0)
        {
            throw new ArgumentException($"{paramName} 不能为空", paramName);
        }

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Pass an array/collection: GetByAnyText(["确认"]) or new[]{ "确认" }.
  2. For JS callers, wrap single values in an array literal.
  3. Verify the interop object type before the call when it originates from a dynamic source.

Example fix

// before (JS)
const loc = page.getByAnyText("确认");

// after
const loc = page.getByAnyText(["确认"]);
Defensive patterns

Strategy: type-guard

Validate before calling

// for JS/interop callers, ensure an array is passed
if (values is string || values is not System.Collections.IEnumerable)
    throw new InvalidOperationException("pass an array of strings");

Type guard

static bool IsCollection(object? o) => o is not null && o is not string && o is System.Collections.IEnumerable;

Prevention

When it happens

Trigger: Calling GetByAnyText("确认") with a single string instead of an array; passing a JS object {a:1}; passing a number; passing a string where a string[] is expected.

Common situations: JavaScript scripts invoking the BgiVision API passing a literal string rather than ["..."]; interop where a single value is wrapped incorrectly; Python/ClearScript returning a dict instead of a list.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/4947446e6a1dee7d. Report an issue: GitHub.