问题
im trying to select an element that's an image withing a div and then click it using nightmare.js. Below is the element im trying to click and below that the code im using.
<div class="custom-navigator-right"><img onload="this.__gwtLastUnhandledEvent="load";" src="http://iris.generali.gr/iris/webiris/clear.cache.gif" style="width:40px;height:43px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAArCAYAAAAKasrDAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oIEQgMJtcPdZsAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAJmUlEQVRYw8WYe4xdVRXGf2uffd937mOmUyjQTlsKlodUbCI1SEyFYIiCYKwQkRgh8ZGoiY8YDT6imKh/aIwxhogxGkUsIFGCIg/FoEhRCxgELIh90KGdR2fu3Jn7OPecvZd/nHunM50nKriTnXvuOffu/Z21vrXWt7bQHV9/aleAcHYgwa5sJrs9ZW2RV2GEUWeyHbYfB+749Lm3P9u7f9MTV/L583+BAHzlyasGSvm+G6vF0seL+SLtThvnffLwFR5iDPlMlulmwx+rT3690Wp+7cbX3VWffX7Tk1fmy/nStwdK1RumGnXGasdUQV4NcAAKiIgOlgekVChSm6nfPDx+5FNf3XFf48uPvwObMnZXIZe/YWRiVKea0wQmeNXAAT0vyWhtTMMolEIu/6HBysCDW77Cz7/w+l9irLWfa3dCeuD4Pw1BpN6Y1rATIsi1733r2ysAFmRLbbqmRoyo6st2kMej+K6zulthMJjj9lntaqrSaLcQkYvS6UwRqNmwExLHDmX14DwOT0ygaUruVAoMkKaAooTM0GSCejCMF0eARTCrj+pOiDGmD03+ZJ13ePWyOnspnpiKG2Ijb2CdfQ25Qp50KkNgAhDBuZhO1KEVNhiOn2E/f6IeHCEghaxgUVXFGIP3fvaeVVVUFVkhNBRPxpfYqpeyOb+dcl+ZQqFALpclZS3GJFby3hPHjna7zSmNDZxZfyPPNx/jWfMbYmmtaM0TaWa99/RALkNgKmx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Xw+Pq3PPqFjR3f+uF0OPHmmU5t9mRAVRf0IvNmV4EfbwsEQRCRJOeZ3nVvmnnfjRHK6TW+lFnzQNpkPnT9ji8e+PT9l3Fw6ml27zqU5MEwjLLi5Yo4dqfWw2PMhHViF+NcjHce51wyfXKd9K3dOdsnzxEEs5sbjDEEQTD7GQQBgTEENiAwAdZaiumyqeQGN4rRS6/RT34/L0U/z1Htdhio6iZV3y9YK5iSel/2+ORTNQBd5513XjUlQr+qplS9+lnGzqGS9JSLIEZiVY4aI0bERCIyGRjjQI4aY0KDOariIo9zIjIWBOmD6XRwHGCr1SaXyy51yhR0A0kAi4LHC6pB0k91NaHSlbI97gpi6LoawMRGBCQ5nOhuEQG62MFpGHbIZNJL1N0l9OErNZxbXhP+G+jjHweFNoBRAAAAAElFTkSuQmCC) no-repeat 0px 0px;" class="gwt-Image" border="0"></div>
.evaluate(()=>{
let divs= document.getElementsByClassName("custom-navigator-right");
for (var div of divs)
div.click();
})
I'm new in nightmare.js and in web scraping thank you in advance.
回答1:
Your code should work fine, the only thing you might want to add if you want to see the browser GUI is either .then() after evaluate or put evaluate inside of .then(). Below is the code that is working and it checks all the checkboxes on the page when you run it.
var Nightmare = require('nightmare')
var nightmare = Nightmare({ show: true })
nightmare
.goto('https://materializecss.com/checkboxes.html')
.wait('label input[type="checkbox"]')
.evaluate(function() {
let checkboxes = document.querySelectorAll('label input[type="checkbox"]')
for (let checkbox of checkboxes) checkbox.click()
})
.then(function() {
console.log('Im here just to show the GUI')
})
or
var Nightmare = require('nightmare')
var nightmare = Nightmare({ show: true })
nightmare
.goto('https://materializecss.com/checkboxes.html')
.wait('label input[type="checkbox"]')
.then(function() {
return nightmare.evaluate(function() {
let checkboxes = document.querySelectorAll('label input[type="checkbox"]')
for (let checkbox of checkboxes) checkbox.click()
})
})
来源:https://stackoverflow.com/questions/51669862/selecting-and-clicking-elements-based-on-class-name-with-nightmare-js