0

I am pulling a list of input elements from a page and would like to determine the "type" of each one.

For instance:

    var elements = await page.QuerySelectorAllAsync("input");
    if (elements != null)
    {
        foreach (var element in elements)
        {
            if (element.GetType().ToString() == "password")
            {
                await element.TypeAsync("password");
            }
        }
    }

However, GetType is not correct. Is there a way to determine the input type from an element?

hardkoded
  • 18,915
  • 3
  • 52
  • 64
Ray
  • 1,413
  • 2
  • 10
  • 12

2 Answers2

1

You will need to check that on the Chromium's side:

var elements = await page.QuerySelectorAllAsync("input");
if (elements != null)
{
    foreach (var element in elements)
    {
        if ((await page.EvaluateFunctionAsync<string>("e => e.type", element)) == "password")
        {
            await element.TypeAsync("password");
        }
    }
}
hardkoded
  • 18,915
  • 3
  • 52
  • 64
  • This causes an exception on the "TypeAsync" line: PuppeteerException: JSHandle is disposed! – Ray Apr 08 '19 at 19:37
0

Found that this works:

                    var type = await currentElement.GetPropertyAsync("type");
                    if (type.ToString() == "JSHandle:password")
                    {
                        await currentElement.TypeAsync("password");
...
Ray
  • 1,413
  • 2
  • 10
  • 12