2

I'm trying to create multiple chrome threads for each item in the list and execute the function for each item from list simultaneously, but having no idea where to even start any help will be much appreciated.

Code Snippet

import sys

def spotify(elem1, elem2, elem3):

    print("proxy: {}, cc: {}, cvc: {}".format(elem1, elem2, elem3))


def get_cc():
    cc = ['5136154545452522', '51365445452823', '51361265424522']
    return cc

def get_cvc():
    cvc = ['734', '690', '734']
    return cvc

def get_proxies():
    proxies = ['51.77.545.171:8080', '51.77.254.171:8080', '51.77.258.82:8080']
    return proxies

proxArr = get_proxies()
ccArr = get_cc()
cvcArr = get_cvc()
yeslist = ['y','yes']

for elem in zip(proxArr, ccArr, cvcArr):
    spotify(elem[0], elem[1], elem[2])
    restart=input("Do you wish to start again: ").lower()
    if restart not in yeslist:
        sys.exit("Exiting")
Lucjan Grzesik
  • 739
  • 5
  • 17
  • Did this thread answer your question? https://stackoverflow.com/questions/49617485/is-it-possible-to-run-multiple-instances-of-one-selenium-test-at-once – Maximilian Peters May 10 '19 at 20:27
  • No it does not I've tried the solution from there and I got this error ```AttributeError: module 'threading' has no attribute 'RLock'``` – Lucjan Grzesik May 10 '19 at 20:37
  • edit I have named python file threading and got this error I've changed it now and the code from the link doesn't produce any error but It is only running one chrome – Lucjan Grzesik May 10 '19 at 21:31

1 Answers1

3

Similar to the answer here, you can start multiple threads of Chrome.

  • Define a function which executes your Selenium code, execute_chrome in this case
  • Add all required arguments to the function definition
  • Pass the arguments as a tuple in your Thread call, e.g. args=(elem, )
  • Save the script with a name which is not identical to another Python package, e.g. my_selenium_tests.py
  • run the script preferably from the command line and not from an interactive environment (e.g. a Jupyter notebook)

    from selenium import webdriver
    import threading
    import random
    import time
    
    number_of_threads = 4
    
    def execute_chrome(url):
        chrome = webdriver.Chrome()
        chrome.get(url)
        time.sleep(1 + random.random() * 5)
        driver.quit()
    
    urls = ('https://www.google.com', 
            'https://www.bing.com', 
            'https://www.duckduckgo.com', 
            'https://www.yahoo.com')
    
    threads = []
    for i in range(number_of_threads):
        t = threading.Thread(target=execute_chrome, args=(urls[i], ))
        t.start()
        threads.append(t)
    
    for thread in threads:
        thread.join()
    
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
  • Thank you so much after all the link that you've sent later was good :) The problem was that I have named python script threading.py – Lucjan Grzesik May 11 '19 at 00:05