I am trying to do Hacker Rank Automation not selecting the desired element

Viewed 51

I am trying to submit the hackerRank website challenge through automation using puppeteer. After the solve challenge button is clicked the code goes on next page but doesn't select the the checkbox element i.e 'Text against custom input'

const puppeteer = require("puppeteer");
const url = "https://www.hackerrank.com/auth/login";

(async function () {
  try {
    const browserInstance = await puppeteer.launch({
      headless: false,
      slowMo: true,
      args: ["--start-fullscreen"],
      defaultViewport: null,
    });
    let newTab = await browserInstance.newPage();
    await newTab.goto(url);
    await newTab.waitForSelector("#input-1");
    await newTab.click("#input-1");
    let email = "some-email.com";
    await newTab.type("#input-1", email, {
      delay: 50,
    });
    let password = "some-password";
    await newTab.waitForSelector("#input-2");
    await newTab.click("#input-2");
    await newTab.type("#input-2", password, {
      delay: 50,
    });
    await newTab.click('button[data-analytics="LoginPassword"]', { delay: 50 });
    await waitAndClick('a[data-attr1="algorithms"]', newTab);
    await waitAndClick('input[value="warmup', newTab);
    let allChallenges = await newTab.$$(
      ".ui-btn.ui-btn-normal.primary-cta.ui-btn-line-primary.ui-btn-styled",
      { delay: 50 }
    );
    await questionSolver(allChallenges[0], newTab);
    console.log("Total Questions", allChallenges.length);
  } catch (error) {
    console.log(error);
  }
})();
async function questionSolver(challenge, page) {
  await challenge.click();
  let inputIsClicked = await waitAndClick(".checkbox-input", page);
  return inputIsClicked;
}

async function waitAndClick(selector, cpage) {
  await cpage.waitForSelector(selector);
  let selectorClicked = cpage.click(selector);
  return selectorClicked;
}

the selected input is not getting selected

1 Answers

This is the solution that worked for me after updating the questionSolver Function as below

async function questionSolver(challenge, page, answer) {
      await Promise.all([challenge.click(), page.waitForNavigation()]);
      await waitAndClick(".checkbox-wrap", page);
      await page.waitForSelector("textarea.custominput");
      await page.type("textarea.custominput", answer, { delay: 10 });
    }
Related