OiO.lk English javascript Puppeteer clicking Reddit login button
javascript

Puppeteer clicking Reddit login button


I’m running into trouble with Puppeteer with selecting a button during a login process. I’m trying to have it log in to Reddit but the login button has no name or ID and there are other buttons that might get selected. It’s also wrapped in other elements so I’m not even sure which one to select. I’ll include my code below. Does anyone know what I can do to press the login button on Reddit.com after it enters the username and password? Selecting the other elements was easy because they have ids.

const puppeteer = require ('puppeteer');

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function loginToReddit(usernameText, passwordText){
    const browser = await puppeteer.launch({
        headless: false,
        defaultViewport: false,
        userDataDir: "./tmp"
    });
    const page = await browser.newPage();
    await page.goto('https://www.reddit.com/');
    await page.click('#login-button');

    sleep(2000).then(() => {
        enterCredentials(usernameText, passwordText, page);
    });
}

async function enterCredentials(usernameText, passwordText, page){
    await page.click('#login-username');
    await page.keyboard.type(usernameText,{
        delay: 50,
    });
    await page.click('#login-password');
    await page.keyboard.type(passwordText,{
        delay: 50,
    });
    sleep(2000).then(() => {
        selectLoginButton(page);
    });
}

async function selectLoginButton(page) {
    page.waitForNavigation();
    //const xp = 'faceplate-tracker button';
    const xp = 'faceplate-tracker > button';
    const el = await page.waitForSelector(xp);
    await el.click();
}

loginToReddit('username', 'password');



You need to sign in to view this answers

Exit mobile version