This is from a world.js in a cucumber puppeteer project.
The first code block causes an error. But if I hard code the selector as in the second code block, there is no error. How do I pass the argument into the arrow function so I don't have to hard code the selector? TIA
Hard coded selector: works
async getOriginalForecastDate(selectorTitle, selectorDate, wait = 0) {
await this.page.waitForSelector(selectorTitle);
await this.page.waitForSelector(selectorDate);
const originalDateStr = await this.page.evaluate(selectorDate => {
let result = document.querySelector('div[class="rollmodel_cal_date"]');
////let result = document.querySelector(selectorDate);
return result.innerText.trim();
});
const originalDate = utils.constructDate(originalDateStr);
return originalDate;
}
Trying to pass selector as an argument: doesn't work
async getOriginalForecastDate(selectorTitle, selectorDate, wait = 0) {
await this.page.waitForSelector(selectorTitle);
await this.page.waitForSelector(selectorDate);
const originalDateStr = await this.page.evaluate(selectorDate => {
/////let result = document.querySelector('div[class="rollmodel_cal_date"]');
let result = document.querySelector(selectorDate);
return result.innerText.trim();
});
const originalDate = utils.constructDate(originalDateStr);
return originalDate;
}
You should pass arguments after evaluate arrow function. like this page.evaluate(pageFunction, ...pageFunction arguments)
. docs.
async getOriginalForecastDate(selectorTitle, selectorDate, wait = 0) {
await this.page.waitForSelector(selectorTitle);
await this.page.waitForSelector(selectorDate);
const originalDateStr = await this.page.evaluate(
(selectorDate, arg1, arg2) => {
/////let result = document.querySelector('div[class="rollmodel_cal_date"]');
let result = document.querySelector(selectorDate);
return result.innerText.trim();
},
selectorDate,
arg1,
arg2,
);
const originalDate = utils.constructDate(originalDateStr);
return originalDate;
}