const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const fetch = require('node-fetch');
puppeteer.use(StealthPlugin());
(async () => {
// Get the port and wsEndpoint values from the API
const response = await fetch('
http://localhost:3001/v1.0/browser_profiles/profile_id/start?automation=1');
const data = await response.json();
const port = data.automation.port;
const wsEndpoint = data.automation.wsEndpoint;
// Connect to the browser
const browser = await puppeteer.connect({
browserWSEndpoint: `ws://127.0.0.1:${port}${wsEndpoint}`
});
// Navigate to YouTube
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 720 });
await page.goto('
https://www.youtube.com/');
// Click on the avatar button to open the menu
await page.click('button#avatar-btn');
// Click on the "Switch account" link in the menu
await switchAccount(page);
// Function to switch YouTube accounts
async function switchAccount(page) {
// Wait for the "Switch account" link to appear
await page.waitForSelector('ytd-compact-link-renderer #content-icon yt-icon path');
// Click on the "Switch account" link
await page.click('//*[@id="items"]/ytd-compact-link-renderer[3]');
// Wait for the accounts menu to appear
await page.waitForSelector('tp-yt-iron-dropdown tp-yt-paper-listbox');
// Click on "Your channel" in the accounts menu
await page.click('tp-yt-iron-dropdown tp-yt-paper-listbox ytd-compact-link-renderer:nth-child(2)');
// Wait for the page to reload
await page.waitForNavigation();
console.log('Switched to "Your channel".');
}
// Close the browser
await browser.close();
})();