-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running multiple steps with Nightmare (need advice about refactoring) #708
Comments
Solved. |
Can you share how you solved this? I'm also trying to loop over several exists() checks |
@pinguinoholdings check out this repo, maybe it can help you. |
Thanks, I've been pounding through that all night. My code is working similar to yours, but it has a lot of nested .then() clauses that I'm hoping to clean up |
Thanks @alfonsomga My use case was logging into a site, and then clicking through an unknown number of "OK" button prompts before I could get to the page I was looking for. Being an unknown number, I couldn't make it work by nesting .then() statements, not to mention how to exit the check. This is most likely not the most elegant solution, but it works. For anyone following along later, hopefully this can provide a start...
|
@pinguinoholdings I would recommend you to use vo control flow library to achieve it. Here's an example code: var Nightmare = require('nightmare')
var vo = require('vo')
vo(run)(function(err, result) {
if (err) throw err
result.forEach(function (text) {
console.log('#result > h3: ', text)
})
})
function *run() {
var nightmare = Nightmare();
yield nightmare
.goto('https://www.example.com/signin')
.type('#login', 'username')
.type('#password', 'password')
.click('#btn')
for (var i = 0; i < 4; i++) {
yield nightmare
.goto('https://www.example.com/page'+i)
.wait(1000)
.evaluate(function(){
return $('#result > h3').text()
})
}
yield nightmare.end()
} As you can see it's pretty simple and straightforward. |
By the way if you use vo control flow library you will can do things like: var okButton = yield nightmare.exists('button[id="OK_Button"]');
if (okButton) {
// ....
} I'm sure this will make your life easier! |
He saved the world |
I don't know if this worked back then, but now you can do the exact same thing just by replacing yield with await in an async function. |
it seems like puppeteer is better than nightmare
|
hey for those looking for this answer, I was able to accomplish it doing it this way with no additional packages
|
Full example here https://gist.github.com/Josiassejod1/587440edcfbd4e90d65ee3f1f6f24fe6 |
Hello!
I would like to know how I could improve the code quality of this example script.
My main programming language isn't JavaScript so I am a bit lost about how I could make it better.
This example Nightmare script goes to
accounts.google.com/signin
and sign a Google account.The script also checks if an element exist and if it not exists then the script will log an error so It can notify me when the bot is broken.
Here's the code:
The text was updated successfully, but these errors were encountered: