0

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);
        });
    }
Zanko
  • 4,298
  • 4
  • 31
  • 54
  • Because you don't let the system handle the click most likely. It doesn't happen synchronously – Sami Kuhmonen Feb 18 '16 at 06:09
  • Yes I believe so. It seems that the console log out put synchronously before the change even happened. Can I alter the code we are sure that html changed before getting the img ? – Zanko Feb 18 '16 at 06:11
  • You could call the onclick handler directly, or hook into the image's events to show when it's loaded maybe – Sami Kuhmonen Feb 18 '16 at 06:12
  • `var elem = document.getElementById("linkid"); if (typeof elem.onclick == "function") { elem.onclick.apply(elem); }` This is the answer from another thread, but after replacing this it still behaves the same – Zanko Feb 18 '16 at 06:13
  • can you share the html part. i think you missing to set index in the below line, var myimg = someimage[0].getElementsByTagName('img')[0]; – Sunil Bharath Feb 18 '16 at 06:53
  • 1
    Possible duplicate of [Simulating a click in jQuery/JavaScript on a link](http://stackoverflow.com/questions/1839363/simulating-a-click-in-jquery-javascript-on-a-link) – John Slegers Feb 18 '16 at 07:33

0 Answers0