I am trying to read data from an excel file using exceljs. I put the data from a cell in the excel file into a variable, then try to use this variable to fill a form of a webpage using Playwright.
This is an extract of the code:
const { chromium } = require("playwright");
const ExcelJS = require('exceljs');
const wb = new ExcelJS.Workbook();
const fileName = 'Book1.xlsx';
wb.xlsx.readFile(fileName).then(() => {
const ws = wb.getWorksheet('RenM1ACont');
var salary = Number(ws.getCell('I69').value.result).toFixed(0).toString();
console.log(salary);
return salary;
}).catch(err => {
console.log(err.message);
});
(async () => {
// launch a new Chromium browser instance
const browser = await chromium.launch({
headless: false,
});
const page = await browser.newPage();
// browser automation for web scraping...
await page.goto("https://someweb");
await page.getByTitle('Field1').fill("123");
await page.getByTitle('Field2').fill("1971");
await page.getByTitle('name.').fill(salary);
I get the error:
salary is not defined.
I'm not sure what site you're automating, so I can't guarantee this will work, but your promise code needs improvement for starters. This allows salary
to be read from the Playwright promise chain:
(async () => {
await wb.xlsx.readFile(fileName);
const ws = wb.getWorksheet("RenM1ACont");
const salary = Number(ws.getCell("I69").value.result)
.toFixed(0)
.toString();
const browser = await chromium.launch({headless: false});
const page = await browser.newPage();
await page.goto("https://someweb");
await page.getByTitle("Field1").fill("123");
await page.getByTitle("Field2").fill("1971");
await page.getByTitle("name.").fill(salary);
})();