Search code examples
c#webautomationpuppeteer-sharp

Get Result of document.querySelectorAll in PuppeteerSharp


Here is my simple code:

var element = await page.EvaluateExpressionAsync($"document.querySelectorAll('[{attributeName}=\"{attributeValue}\"]')[0];");

it will work correctly on the page, but it returns null. where is the problem? I know it will execute correctly but I can not detect it on code.


Solution

  • If you want to get an element from the browser you should get an ElementHandle, which is a pointer to an element in the browser, using EvaluateExpressionHandleAsync:

    var element = await page.EvaluateExpressionHandleAsync($"document.querySelectorAll('[{attributeName}=\"{attributeValue}\"]')[0];");