我从select -> option中选择选项,每次页面重新加载时,首先,当我单击下拉列表时,我会收集所有要选择的elementHandles。我想遍历所有这些。
我只能选择第一个,然后出现此错误:



因此,我试图在每次页面重新加载时重新创建ElementHandles。我有以下代码:

函数1:

case:click
try {
   await page.evaluate((el) => {
   return el.click()
   }, 'select');
   await page.waitFor(1500);
} catch (e) {
   console.log(e);
}
break;

case: getNavigation
let navigation = await page.$$('select > option');
break;

case: doActions
let i = 0;
for (elements in navigation) {
  let result = await function2(commands, i, page)
  i++;
}
break;

然后函数2:
async function function2(commands, i, inPage){
let page = inPage;
if (!page) {
const browser = await puppeteerLambda.getBrowser({ headless: true, slowMo: 100,  args: ['--no-sandbox', '--disable-setuid-sandbox', '--single-process', '--start-fullscreen', '--window-size=1413,749']}); //TODO: setup Proxy
        console.log('opening new page');
        page = await browser.newPage();
....
}
let navigation;
case: click
try {
await page.evaluate((el) => {
return el.click()
}, 'select');
await page.waitFor(1500);
} catch (e) {
console.log(e);
}
case: getNavigation
navigation = await page.$$('select > option'); //recreating elementHandle array

case: selectOption
const optionValue = await page.evaluate(value => value.value, navigation[i]);
await page.select('select', optionValue);
case: extract
......

再次,我进入选择选项两次,然后出现此错误:



谁能帮我解决这个问题?

最佳答案

根据Official DocumentationJSHandle:



因此,每次页面重新加载时,您都需要重新获取JSHandle

10-06 13:47