无法点击对话框中的保存按钮

问题描述 投票:0回答:1

我正在使用 NodeJS 和 Playwright 做一个 Web 自动化项目。该项目是抓取链接并在新选项卡中打开它并保存 PDF。我到达了保存 PDF 的最后一部分,但我卡在了这一步。我尝试了多种方法,但无法单击对话框中的“保存”按钮,如图所示。如果您能帮我解决这个问题,我将不胜感激。谢谢你。

我尝试如下模拟 Enter 按键:

await page.keyboard.press('Enter'); 

另外,我尝试在对话框中找到保存按钮并单击它,如下所示:

// Wait for the print dialog to appear and interact with it
const printDialog = await context.waitForEvent('page');
await printDialog.waitForSelector('select.md-select');
await printDialog.selectOption('select.md-select', { label: 'Save as PDF' });

//点击保存按钮

await printDialog.click('cr-button.action-button'); 
两种方法都行不通

我想要做的就是在出现打印/保存对话框时单击保存按钮。第一个对话框出现在浏览器中;第二个出现在操作系统的浏览器之外。

下面是处理在新选项卡中打开收集的链接的函数,如果导航成功,它将单击 id 为“btn-print”的网页上的按钮并在浏览器中打开对话框:

async function handlePdfLink(url, name) {
    const pdfPage = await context.newPage();
    const maxRetries = 4;
    let navigationSuccess = false;

    for (let attempt = 1; attempt <= maxRetries; attempt++) {
      try {
        console.log(`Attempt ${attempt} to load ${url}`);
        await pdfPage.goto(url, { waitUntil: 'networkidle', timeout: 60000 });
        navigationSuccess = true;
        break;
      } catch (error) {
        console.warn(`Attempt ${attempt} failed to load ${url}:`, error);
        if (attempt === maxRetries) {
          console.error(`Failed to load ${url} after ${maxRetries} attempts.`);
          failedUrls.push(url); // Add URL to failedUrls
        } else {
          await pdfPage.waitForTimeout(1000);
        }
      }
    }

    if (navigationSuccess) {
      try {
        // Wait for iframe and get its content frame
        const iframeElementHandle = await pdfPage.waitForSelector('iframe');
        const frame = await iframeElementHandle.contentFrame();

        if (!frame) {
          throw new Error('Could not get iframe content.');
        }

        // Click the print button inside the frame
        await frame.click('#btn-print');

        // Wait for the print dialog to appear and interact with it
        const printDialog = await context.waitForEvent('page');
        await printDialog.waitForSelector('select.md-select');
        await printDialog.selectOption('select.md-select', { label: 'Save as PDF' });

        // Click the save button
        await printDialog.click('cr-button.action-button');

        // Wait for the download to complete and save the file
        const download = await context.waitForEvent('download');
        const savePath = path.join(pdfDir, generateUniqueFileName(name));
        await download.saveAs(savePath);

        console.log(`Saved ${savePath}`);
      } catch (error) {
        console.error(`Error processing ${url}:`, error);
        failedUrls.push(url); // Add URL to failedUrls if an error occurs during processing
      }
    }

    await pdfPage.close();
    await new Promise(resolve => setTimeout(resolve, 2000));
  }
node.js web-scraping playwright webautomation playwright-java
1个回答
0
投票

据我所知,Playwright 会忽略对话框,因此您需要以不同的方式处理对话框。我从来没有机会使用这个,但它记录在这里:https://playwright.dev/docs/dialogs

© www.soinside.com 2019 - 2024. All rights reserved.