microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.36k stars 3.71k forks source link

Error: Getting an instant Protocol Error when trying to launch browser #1887

Closed BananaCrazy closed 4 years ago

BananaCrazy commented 4 years ago

Context:

Code Snippet

const playwright = require('playwright');
const cookiePc = { name: 'platform', value: 'pc', domain: 'www.futbin.com', path: '/' };
module.exports = async (version, name, msg) => {
const url = `https://www.futbin.com/players?page=1&search=${name}&version=${searchversion}`;
//url example = https://www.futbin.com/players?page=1&search=maradona&version=mid_icons
try {
    const browser = await playwright.chromium.launch();
    const context = await browser.newContext();
    const browserPage = await context.newPage();
    await context.addCookies([cookiePc]);
    await browserPage.goto(url);

    const table = '#repTb tbody tr';
    const playername = await browserPage.$eval(`${table} .player_name_players_table`, el => el.textContent);
    const rating = await browserPage.$eval(`${table} td:nth-of-type(2) span`, el => el.textContent);
    const position = await browserPage.$eval(`${table} td:nth-of-type(3)`, el => el.textContent);
    const price = await browserPage.$eval(`${table} td:nth-of-type(5) span`, el => el.textContent);
    const sm = await browserPage.$eval(`${table} td:nth-of-type(6)`, el => el.textContent);
    const wf = await browserPage.$eval(`${table} td:nth-of-type(7)`, el => el.textContent);
    const awr = await browserPage.$eval(`${table} td:nth-of-type(8) span:first-of-type`, el => el.textContent);
    const dwr = await browserPage.$eval(`${table} td:nth-of-type(8) span:last-of-type`, el => el.textContent);
    const linkp = await browserPage.$eval(`${table} .player_name_players_table`, el => el.href);
    await browserPage.goto(linkp);
    await browserPage.waitFor(750);
    const lastUpdate = await browserPage.$eval('#pc-updated', el => el.textContent);
    const chemStyle = await browserPage.$eval('.pgp_chem_val_tooltip', el => el.textContent);
    const imgPlayer = await browserPage.$eval('#player_pic', img => img.src);
    const adsSave = await browserPage.$('.banner_save--3Xnwp');
    await adsSave.click();
    const cookieClick = await browserPage.$('.cc-compliance a');
    await cookieClick.click();
    const graphClick = await browserPage.$('.highcharts-range-selector-buttons g:nth-of-type(2)');
    await graphClick.click();
    const graph = await browserPage.$('#daily_graph .highcharts-container');
    await graph.screenshot({ path: './src/commands/futbin/icons/graph.jpg', type: 'jpeg', quality: 75 });
    await browser.close();
} catch (err) {
    console.error(`${err}. Icon command.`);
    lastMsg.then(msg => msg.delete());
    return msg.channel.send(msgEmbed.setTitle('An error has occurred, retry the command. If the error keeps happening contact @Staff'));
         }
};

The error that is being logged: Error: Protocol error (Target.setAutoAttach): Target closed..

This is for a custom command on a Discord bot, when I was testing locally this worked perfectly fine. Almost never got any errors.

The moment I deployed my code to Heroku so it can be online all the time it gave me an instant error like this, and the error message happens almost instantly after the command was typed in so it must be happening very early in the process. Not sure if this is a Playwright error or something to do with Heroku but any help here would be very appreciated.

arjunattam commented 4 years ago

Hey @BananaCrazy, I noticed your code in this repo. Unfortunately the default Heroku web dyno cannot run a headless browser. You can refer to our Docker setup to install the dependencies required to run a browser on Heroku. Since Heroku can run Docker, this should unblock you.