简体   繁体   English

如何在 puppeteer 中返回重定向链接的值?

[英]How do I return the value of a redirected link in puppeteer?

Goal: I am trying to finish the first step in the authentication process for a website (api).目标:我正在尝试完成网站 (api) 身份验证过程的第一步。 To do so, the last step is to obtain a code that is in the url (address bar) once it has redirected (this is my guess).为此,最后一步是在重定向后获取 url(地址栏)中的代码(这是我的猜测)。

In short, I have figured out how to input the username / password.简而言之,我已经弄清楚了如何输入用户名/密码。 From there it loads a new page, and then that page loads another page (my best guess is that it checks to ensure all the information is correct) which holds a code in the url.从那里它加载一个新页面,然后该页面加载另一个页面(我最好的猜测是它会检查以确保所有信息都是正确的),该页面包含 url 中的代码。

var puppeteer = require("puppeteer");
let clientInfo = require("./Client_Information.json");

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(
    `https://url-goes-here.com?response_type=code&client_id=${clientInfo.client_id}`
  );
  await page.screenshot({ path: "./ScreenShots/ScreenShot.png" });
  // Enter username GOOD
  await page.click("#user_id");
  await page.keyboard.type(clientInfo.user_name);
  await page.screenshot({ path: "./ScreenShots/ScreenShot1.png" });
  console.log("Username has been inserted");
  // Accepting Username GOOD
  await page.click(".andes-button--large");
  await page.screenshot({ path: "./ScreenShots/ScreenShot2.png" });
  await page.keyboard.press("Accept"); //Enter Key
  await page.screenshot({ path: "./ScreenShots/ScreenShot3.png" });
  //Delay 1.5 Seconds
  await page.waitForTimeout(1000);
  // Enter Password GOOD
  await page.click("#password");
  await page.keyboard.type(clientInfo.password);
  await page.screenshot({ path: "./ScreenShots/ScreenShot4.png" });
  console.log("Password has been inserted");
  // Accepting Password GOOD
  await page.click("#action-complete");
  await page.screenshot({ path: "./ScreenShots/ScreenShot5.png" });
  await page.keyboard.press("Accept"); //Enter Key
  await page.screenshot({ path: "./ScreenShots/ScreenShot6.png" });
  //Delay 4 Seconds
  await page.waitForNavigation();
  await page.waitForTimeout(1000);
  //URL Fetch GOOD
  const url1 = await page.url();
  console.log("Page URL : " + url1);
  //Store URL
  clientInfo.Step1_Url = url1;
  //NOTE New Section
  await page.waitForTimeout(10000);
  const url2 = await page.url();
  console.log("Page URL : " + url2);
  await browser.close();
})();

In the //NOTE New Section area is where I am waiting to get the actual resolve redirect, though instead I get chrome-error://chromewebdata/ vs the expected url+code.在 //NOTE New Section 区域中,我正在等待获得实际的解析重定向,但我得到的是chrome-error://chromewebdata/与预期的 url+code。 Thoughts?想法?

the Issue was with the redirect_uri.问题在于redirect_uri。 I had it set to localhost and that seemingly was causing puppeteer to not be able to retrieve it.我将它设置为 localhost ,这似乎导致 puppeteer 无法检索它。 Upon changing this to heroku, it worked instantly.将其更改为 heroku 后,它立即工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM