简体   繁体   中英

How to click a lot of elements in Javascript without errors?

I have been trying to download all USA and CANADA servers here on Nord VPN website: https://nordvpn.com/ovpn/

I tried to manually download it but it is time consuming to scroll down every time and identify each US related servers, so i just wrote simple Javascript that can be run on Chrome Inspect Element Console:

var servers = document.getElementsByClassName("mr-2");
var inc_servers = [];
for (var i = 0; i < servers.length; i++) {
  var main_server = servers[i];
  var server = servers[i].innerText;
  if(server.includes("ca")){
        var parent_server = main_server.parentElement.parentElement;
        parent_server.querySelector(".Button.Button--primary.Button--small").click();
        inc_servers.push(server);
  }
}
console.log(JSON.stringify(inc_servers));

I also wrote simple python script that automatically click "save" file:

while True:
    try:
        app2 = pywinauto.Application().connect(title=u'Save As', found_index=0)
        app2.SaveAs.Save.click()
    except:
        pass

It gets all the elements, it works there, however, when I let javascript click each element, maybe because of too many elements, it returns an error:

VM15575:8 Throttling navigation to prevent the browser from hanging. See https://crbug.com/1038223 . Command line switch --disable-ipc-flooding-protection can be used to bypass the protection

Are there any other best alternative for my problem? Or maybe how to fix the error message above? I tried running this command in my command prompt: switch --disable-ipc-flooding-protection

but it returns also an error: 'switch' is not recognized as an internal or external command, operable program or batch file.

I only know basic Javascript and Python. Thanks

So right off the bat, your program is simply downloading files too fast.

Adding a small delay between each file download allows your JavaScript to run.

var servers = document.getElementsByClassName("mr-2");
var inc_servers = [];
for (var i = 0; i < servers.length; i++) {
  var main_server = servers[i];
  var server = servers[i].innerText;
  if(server.includes("ca")){
        var parent_server = main_server.parentElement.parentElement;
        // Add 1 second delay between each download (fast enough on my computer.. Might be too fast for yours.)
        await new Promise(resolve => setTimeout(resolve, 1000));
        parent_server.querySelector(".Button.Button--primary.Button--small").click();


  }
}
// Remove the logging. Just tell the user that it's worked
console.log("Done downloading all files.");

This is more of a temporary solution, but this script seems like it only needs to be run once, so it'll work for now.

(your python code runs fine. Nothing to do there)

Hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM