如何使用Puppeteer点击Cloud Saas应用程序上的链接?

时间:2018-06-06 16:51:27

标签: javascript 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.

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

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'));

在Target Changed(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,不知道为什么。

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

0 个答案:

没有答案