I am trying to parse data from a website
var inputs = document.getElementsByClassName('parentClass');
for (var i = 0; i < inputs.length; i++) {
inputs[i].click();
var someimage = document.getElementsByClassName('class-img');
var myimg = someimage[0].getElementsByTagName('img')[0];
var mysrc = myimg.src;
console.log(mysrc);
}
The website has this behavior, whenever you click the div, it triggers to onclick and as a result, different image will be shown. I am writing this javascript to get all the image in order from top to bottom. That is why i am simulating the click. The problem with this is that it somehow does not grasp the new link but the last link. There are 19 images and it is printing the last image src 19 times. Any clue?
I ended up have to go through the page source and manually get the onclick argument so I can use AJAX to send the post request
var inputs = document.getElementsByClassName('parentClass');
for (var i = 0; i < inputs.length; i++) {
var str = (inputs[i].getAttribute('onclick'));
var params = str.split("nameOfFunction(")[1].split(',');
params[params.length - 1] = params[params.length - 1].replace(');');
$.ajax({
method: "POST",
url: "request.php",
data: {
id: parseInt(params[0]),
name: params[1],
belongsys: params[2],
type: params[3],
rep: params[4],
set: params[5],
mass: ' kg'
}
}).done(function(result){
$(".class-img").html(result).show();
var someimage = document.getElementsByClassName('class-img');
var myimg = someimage[0].getElementsByTagName('img')[0];
var mysrc = myimg.src;
console.log(mysrc)
array.push(mysrc);
});
}