I am using Puppeteer to build an automated shopping bot. I have made this function addToCart to add a product to the cart. After adding the product, I want to open a new url in the same browser session but it's not working.
const puppeteer = require("puppeteer");
const product_url =
"https://www.amazon.com/gp/product/B08ZL7LZW3?pf_rd_r=J6QNRZDRJ7Z8FSF2HVXR&pf_rd_p=6fc81c8c-2a38-41c6-a68a-f78c79e7253f&pd_rd_r=d3adff00-5e6f-4456-8fd0-bd187f2ff86d&pd_rd_w=oO6kF&pd_rd_wg=aoGGq&ref_=pd_gw_unk";
const checkout_url = "https://www.amazon.com/gp/buy/shipoptionselect/handlers/display.html?hasWorkingJavascript=1";
async function givePage() {
const browser = await puppeteer.launch({
headless: false,
});
const page = await browser.newPage();
return page;
}
async function addToCart(page) {
await page.goto(product_url);
await page.waitForSelector(
"button[class='single_add_to_cart_button button alt']"
);
await page.click(
"button[class='single_add_to_cart_button button alt']",
(elem) => elem.click()
);
}
async function checkout() {
var page = await givePage();
await addToCart(page);
await page.waitForNavigation();
const page2 = await browser.newPage(); // open new tab
await page2.goto("https://www.amazon.com/gp/buy/shipoptionselect/handlers/display.html?hasWorkingJavascript=1"); // go to github.com
await page2.bringToFront(); // make the tab active
}
checkout();