Selenium create multiple chrome threads for each i

2019-07-10 07:19发布

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")

1条回答
叼着烟拽天下
2楼-- · 2019-07-10 07:43

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()
    
查看更多
登录 后发表回答