首页 > 解决方案 > 如何使用 Puppeteer 单击 Cloud Saas 应用程序上的链接?

问题描述

我正在尝试使用 puppeteer 在 Oracle Cloud Saas ERP 应用程序上自动化一些数据输入和测试。

以下是步骤和代码。我遇到的问题是,每当我执行 page.click 元素时,我都会收到错误消息:

(node:58569) UnhandledPromiseRejectionWarning: Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': 'a#pt1:commandLink1.xj8.xit' is not a valid selector.
    at <anonymous>:1:33
    at ExecutionContext.evaluateHandle (/Users/user/node_modules/puppeteer/lib/ExecutionContext.js:88:13)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)
(node:58569) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:58569) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

打开登录页面并登录到应用程序(作品)

const browser = await puppeteer.launch({headless: false});
  let page = await browser.newPage();
  await page.goto(config.appurl, {waitUntil: 'networkidle2'});
  await page.type('#userid', 'USERNAME', {delay : 10});
  await page.type('#password', 'PASSWORD', {delay : 10});
  await page.waitForSelector('#btnActive', {enabled : true});
  page.click('#btnActive', {delay : 1000}).then(() => console.log('Login Button Clicked'));

目标更改的日志 url (Works)

browser.on('targetchanged', ()=> {
    console.log('Target Changed!');
    console.log('URL: ', page.url());
  });

渲染,屏幕截图并单击主页加载时的元素(渲染和屏幕截图按预期工作。我看到屏幕截图和渲染的 html 是用户登录后的主页,但点击会产生错误 - 不是有效的选择器)

page.on('load',()=> {
    console.log('Home Page Loaded!');
    console.log(page.url());
    page.screenshot({path: 'home.png'});
    async function render(page){
      let html = await page.content();
      await fs.writeFileSync('home.html', html);
    }
    render(page);
    page.click('a#pt1:commandLink1.xj8.xit', {delay : 1000}).then(() => console.log('Link Clicked')); //this is what generates the error
  });

从主页的呈现内容中,我看到下面的 html 片段,这是我尝试使用 page.click 进行单击的内容。

<div><a id="pt1:commandLink1" class="xj8 xit" onclick="return false;" href="#">You have a new home page!</a></div>

我看到使用 puppeteer 和 chrome 在 Cloud Saas App 上编写脚本的另一个问题是我无法在页面上的所有元素上使用 Inspect Element,不知道为什么。

如何更改我的脚本以便能够单击此页面上提到的链接?

标签: javascriptpuppeteer

解决方案


推荐阅读