2

I'm trying to use puppeteer to save data on website from a excel.Fisrt click the add button on the page,It will navigate to the add form, then extract data from excel file.And type the data in each input field, click save.It will redirect to the list page.After that click add button again.

Here's part of my code:

     readExcel().then(async data => {
       //Read data from excel line by line
       for (let row = 1; row < 5; row++) {
       let from = 'A' + row;
       let to = 'B' + row;
       //get Value1
       let fromValue = data[from].v;
       //get Value2
       let toValue = data[to].v;


       let addButton = await page.$('.action-links a');
       await addButton.click();
       await page.waitForNavigation({ waitUntil: 'domcontentloaded' 
       }).then(async () => {
       let fromUrl = await page.$('#edit-source');
       let toUrl = await page.$('#edit-redirect');
       let save = await page.$('#edit-submit');
       await fromUrl.type(fromValue);
       await toUrl.type(toValue);
       await save.click();
     });
    }

It only save first line of data.Reported error: UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation. Anyone can help me?

Shawn Rong
  • 145
  • 3
  • 14
  • most likely this is happening because when you click the save button the next loop starts immediately and your page hasn't loaded yet. You need to wait until find that your page has fully loaded after submission. I suppose that is your case but you need to provide more details of your problem. – Eleftherios Dec 30 '18 at 19:59
  • See [this answer](https://stackoverflow.com/questions/50658786/how-do-i-reference-the-current-page-object-in-puppeteer-once-user-moves-from-log/74659952#74659952). Your `page.click()` and `page.waitForNavigation()` aren't awaited properly. There may be other problems, so a [mcve] is always ideal. – ggorlen Dec 02 '22 at 18:32

0 Answers0