Я пытаюсь войти в систему и для этого сделал функцию:
async function login(page, usernameXPATH, passwordXPATH, username, password)
{
const usernameElement = await page.$x(usernameXPATH);
await usernameElement[0].click({clickCount: 3});
await page.type(usernameElement[0], username);
const passwordElement = await page.$x(passwordXPATH);
await passwordElement[0].click({clickCount: 3});
await page.type(passwordElement[0], password);
await page.keyboard.press('Enter');
}
Но это вызывает ошибку
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'BrowserContext'
| property '_browser' -> object with constructor 'Browser'
--- property '_defaultContext' closes the circle Are you passing a nested JSHandle?
at JSON.stringify (<anonymous>)
at Connection._rawSend (node_modules/puppeteer/src/common/Connection.ts:100:37)
at CDPSession.send (node_modules/puppeteer/src/common/Connection.ts:259:33)
at ExecutionContext._evaluateInternal (node_modules/puppeteer/src/common/ExecutionContext.ts:252:44)
at ExecutionContext.evaluateHandle (node_modules/puppeteer/src/common/ExecutionContext.ts:191:17)
at ElementHandle.evaluateHandle (node_modules/puppeteer/src/common/JSHandle.ts:183:42)
at Object.internalHandler.queryOne (node_modules/puppeteer/src/common/QueryHandler.ts:68:38)
at ElementHandle.$ (node_modules/puppeteer/src/common/JSHandle.ts:778:25)
at DOMWorld.$ (node_modules/puppeteer/src/common/DOMWorld.ts:171:34)
Если я удалю [0] и использую только usernameElement.click(), то я получаю сообщение об ошибке, что click() не является функцией.
Что я здесь делаю не так?
Это простой HTML-элемент ... className: 'HTMLInputElement'



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Page.type принимает селектор в качестве своего первого аргумента, а не элемент (соответствующий XPath). Скорее используйте elementHandle.type, например:
async function login(page, usernameXPATH, passwordXPATH, username, password)
{
const usernameElement = await page.$x(usernameXPATH);
await usernameElement[0].click({clickCount: 3});
await usernameElement[0].type(username);
const passwordElement = await page.$x(passwordXPATH);
await passwordElement[0].click({clickCount: 3});
await passwordElement[0].type(password);
await page.keyboard.press('Enter');
}
Объект сообщения, переданный в github.com/puppeteer/puppeteer/blob/…, кажется, имеет внутри циклическую ссылку. Может быть, попробуйте поставить точку останова, чтобы увидеть переданное значение.