'Puppeteer / Node.js - Submit login form > resubmit same login form when and only if redirected to resubmit form from main page. (redirect is random)
So I'm new to puppeteer/node/js so I need to read a lot of docs to become familiar but in the haze of it all it does not always obvious what and how to implement the correct method.
I have a sequence of events to navigate where I am stuck is section 2A of submitting the login form. Sometimes I am required to login just once. I can handle the logging in but sometimes I am redirected to a main page and on the next step (a button click after first login on the main page) I am redirected back to the login form again.
await page.goto("https://homepage/", {
waitUntil: `networkidle2`,
})
// 1 - Go to User Login: user login button - this works
const [response] = await Promise.all([
page.waitForNavigation({
delay: randomGenerator * 200,
}),
page.click(`.access`, {
waitUntil: `networkidle2`,
}),
])
// 2A - Login Page - this works
await page.type("#User", process.env.KEY, {
delay: randomGenerator,
})
// password
await page.type("#Pass", process.env.PASSWORD, {
delay: randomGenerator,
})
// 3 - Login Submit Button - is good if main page does not redirect to step 2
await page.click(`button[type=submission][name=submitBtn]`, {
delay: randomGenerator,
}),
// 4 - this section sometimes redirects to step 2 Login Page
await page.$(
'div[id=mainPage][name=mainPage"]'
),
// 5 - Below is the code that is not functioning. I tried to overcome the random
redirect. I tried
`waitForNavigation` and `waitForSelector`, perhaps I was on the
right track but not too sure. I get unknown value error when with
`waitForNavigation` and I am not handling `waitForSelector` would work
but gives error if there is not a login page.
// 2B - Login Page attempt if redirected from main page
// This step is not always enforced and when enforced follows step 3
await page.waitForSelector("#User")
if ((await page.$("#User")) !== null) {
await page.type("#User", process.env.KEY, {
delay: randomGenerator,
})
setInterval(
await page.type("#Pass", process.env.PASSWORD, {
delay: randomGenerator,
}),
2000
)
}
`Error: waiting for selector #User failed: timeout 300`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
