Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer cors mistake

hello i have problem in my code using puppeteer, cors error happens randomly but in 80% of my tests. Here is my code thanks for help.Btw the server respond is

Access to fetch at 'https://secure-store.nike.com/eu/services/jcartService/?action=addItem&rt=json&country=GB&region=eu&lang_locale=en_GB&catalogId=1&productId=12238990&qty=1&skuId=21502246' from origin 'https://www.nike.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

const puppeteer = require('puppeteer');
const jsonfile = require('jsonfile')

function evaluate_click(element,page){
  page.evaluate((el) => {
     el.click()
  },element);
}

async function bot(){
const browser = await puppeteer.launch({headless: false})
const page = await browser.newPage()
setTimeout(function(){
    browser.close()
},120000)
await page.goto('https://www.nike.com/gb/launch/t/air-max-deluxe-midnight-navy-laser-orange-persian-violet/')
await page.waitForSelector('button.ncss-btn-accent.ncss-brand.pt3-sm.pb3-sm.pt2-lg.pb2-lg.u-uppercase.ta-sm-c.u-full-width')
await page.click('button.ncss-btn-accent.ncss-brand.pt3-sm.pb3-sm.pt2-lg.pb2-lg.u-uppercase.ta-sm-c.u-full-width')
await page.waitFor(1000)
await page.waitForSelector('button[aria-haspopup="true"]')
await page.click('button[aria-haspopup="true"]')
await page.waitForXPath("//ul[contains(@class,'')]//li[11]//button[1]")
var select_size = await page.$x("//ul[contains(@class,'')]//li[11]//button[1]")
await evaluate_click(select_size[0],page)
await page.waitFor(1000)
await page.waitForSelector('button.ncss-brand.ncss-btn-black.pb3-sm.prl5-sm.pt3-sm.u-uppercase.u-full-width')
await page.click('button.ncss-brand.ncss-btn-black.pb3-sm.prl5-sm.pt3-sm.u-uppercase.u-full-width')
await page.waitForSelector('a[data-qa="checkout-link"]')
await page.click('a[data-qa="checkout-link"]')

}

bot()

like image 529
bredart Avatar asked Sep 01 '18 15:09

bredart


People also ask

How do you fix the CORS error in angular app?

We call this the CORS error. CORS error due to browser's same origin policy. To get around this, you need to tell your browser to enable your client and your server to share resources while being of different origins. In other words, you need to enable cross-origin resource sharing or CORS in your application.

How do I enable CORS in angular 2?

Sending the Origin header in the original request will enable the CORS support on the server side. This header is automatically added by the browser when it detects that the domain of the request isn't the same as the caller's.

What is CORS policy in angular?

CORS is an HTTP header- based mechanism that allows the server to indicate any other domain, scheme, or port origins and enables them to be loaded.


2 Answers

You can pass the --disable-web-security flag to puppeteer.launch() to disable web security:

const browser = await puppeteer.launch({
  args: [
    '--disable-web-security',
  ],
  headless: false,
});
like image 152
Grant Miller Avatar answered Sep 21 '22 11:09

Grant Miller


Nowadays these flags are needed:

"--disable-features=IsolateOrigins", "--disable-site-isolation-trials"
await puppeteer.launch({
    headless: headless,
    devtools: true,
    args: [
        '--disable-web-security',
        '--disable-features=IsolateOrigins',
        '--disable-site-isolation-trials'
    ]
});

Also make sure you have a recent version of puppeteer, since it crashes with these flags in [email protected].

You can check that isolation is disabled in: chrome://process-internals

Suggested in https://stackoverflow.com/a/51320323/337587

More information on the flag: https://www.chromium.org/Home/chromium-security/site-isolation

like image 32
james Avatar answered Sep 21 '22 11:09

james