Fix

//=======================================================================
// Indeed Auto-Applier
// ----------------------------------------------------------------------


// Importing necessary modules
const { chromium } = require('playwright-extra');
const stealth = require('puppeteer-extra-plugin-stealth')();

// Adding stealth plugin to the chromium browser
chromium.use(stealth);

// Importing the promises API of the file system module
const fs = require('fs/promises');

// Setting up variables
const email = 'tnwpvdzakkpamyduti@ckptr.com';
const password = 'login123';
const amount = 10;
const transportation = 'Yes';
const authorized = 'Yes';
const flexibleSchedule = 'Yes';
const yearsOfSalesExperience = '1';
const yearsOfRetailExperience = '1';
const fullTime = 'Yes';
const commute = 'Yes';
const b2bSales = 'No';
const accountManagement = 'No';

// Setting up search parameters
const search = 'test';
const location = 'Brea, CA';
const radius = 5;
const sorting = 'date';

let jobs = []; // Array to store job URLs

// Function to generate Indeed URL with search parameters
function generateIndeedURL(search, location, radius, sorting) {
    const encodedSearch = encodeURIComponent(search);
    const encodedLocation = encodeURIComponent(location);
    return `https://www.indeed.com/jobs?q=${encodedSearch}&l=${encodedLocation}&radius=${radius}&sort=${sorting}`;
}

// Generating Indeed URL
const indeedURL = generateIndeedURL(search, location, radius, sorting);
console.log('Indeed URL:', indeedURL);

let heading; // Variable to store page heading
let url; // Variable to store page URL

// Async function to automate the job application process
(async () => {
    // Launching the chromium browser with a persistent context
    const browser = await chromium.launchPersistentContext('sessionData', {
        headless: false // Set headless option as needed
    });
    console.log('Browser started');

    // Opening a new blank page
    const page = await browser.newPage();

    try {
        // Navigating to the Indeed website
        await page.goto(indeedURL);

        // Calculating the number of pages to scrape
        let pageCount = Math.floor(amount / 10);
        console.log('Page Count:', pageCount);

        // Looping through each page
        while (pageCount > 0) {
            // Get all links from the page
            const links = await page.$$('a');

            // Extract href attributes from the links
            for (const link of links) {
                const href = await link.getAttribute('href');
                if (href && href.includes('clk')) {
                    jobs.push('https://indeed.com' + href);
                }
            }

            console.log('Job URLs:', jobs);

            // Navigate to the next page if available
            if (pageCount > 1) {
                try {
                    await page.press('[aria-label="Next Page"]');
                } catch (error) {
                    console.error('Error occurred while navigating to the next page:', error);
                }
            }
            pageCount--;
        }

        jobs.forEach(async(job) => {
            
            await page.goto(job);
            
            // wait for the navigation to complete
            await page.waitForNavigation();


            // Loop until "Submit your application" button is clicked
            while (true) {
            
            
                // Get page heading and URL
                heading = await page.$eval('h1', heading => heading.innerText);

                url = page.url();

                console.log('Page Heading:', heading);
                console.log('Page URL:', url);

                // Job Information Page
                if (url.includes('viewjob')) {

                    await page.getByText('Apply now').click()
                
                // Add A Resume Page
                } else if (heading.includes('Add a resume')) {

                    document.$('[data-testid="IndeedResumeCard-indicator"]').click();

                    await page.getByText('Continue').click();
                
                // Certifications Page
                } else if (heading.includes('certifications')) {
                
                    await page.getByText('Continue').click()
                
                } else if (heading.includes('qualifications')) {

                    let fields = await page.$$('fieldset');

                    fields.forEach((field) => {

                        let qText = field.$('legend').innerText;

                       console.log('Qualification: ' + qText);

                    
                        let radioFields = field.$$('label');


                        radioFields.forEach((field) => {


                            let fieldText = field.innerText;

                            console.log('Field Text: ' + fieldText);


                            let input = field.$('input');


                            if (qText.includes('Account management')) {

                                if (fieldText.includes(accountManagement)) {

                                    input.click();

                                }


                            } else if (qText.includes('B2B sales')) {

                                if (fieldText.includes(b2bSales)) {

                                    input.click();
                            }
                        }
                    });
                });

                    await page.getByText('Continue').click();

                // Questions page
                } else if (heading.includes('questions')) {

                    let questions = await page.$$('.ia-Questions-item');
                
                    questions.forEach((question) => {
                    
                        let fields = question.$('fieldset');
                        let qText = question.innerText;

                        if (fields) {
                        let radioFields = fields.$$('label');

                        if (radioFields.length > 0) {

                            radioFields.forEach((field) => {

                                let fieldText = field.innerText;
                                let input = field.$('input');

                                if (qText.includes('License') && fieldText.includes('Yes')) {

                                    input.click();

                                } else if (qText.includes('education') && fieldText.includes('High school')) {

                                    input.click();

                                } else if (qText.includes('authorized') && fieldText.includes('Yes')) {

                                    input.click();

                                } else if (qText.includes('relocate') && fieldText.includes('Yes')) {

                                    input.click();
                                }
                            });
                        }

                    } else {

                        let inputField = question.$('[autocomplete="off"]');

                        if (qText.includes('transportation')) {

                            inputField.fill(transportation);

                        } else if (qText.includes('authorized')) {

                            inputField.fill(authorized);

                        } else if (qText.includes('flexible schedule')) {

                            inputField.fill(flexibleSchedule);

                        } else if (qText.includes('full-time')) {

                            inputField.fill(fullTime);

                        } else if (qText.includes('commute')) {

                            inputField.fill(commute);
                        }
                    }
                });

                    await page.getByText('Continue').click();

                // Skills Page
                } else if (heading.includes('skills') || heading.includes('relevant experience')) {

                    await page.getByText('Continue').click();
                
                // Application Review Page 
                } else if (heading.includes('review your application')) {

                    await page.getByText('Submit your application').click()

                } else {

                    console.log(heading);   
                }
            }
        });
    } catch (error) {    

            console.error('Error occurred:', error);


    } finally {


        await browser.close();

    }
})();