Selenium create multiple chrome threads for each item in array(list) and execute function simultaneously

后端 未结 1 543
甜味超标
甜味超标 2021-01-22 17:53

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 a

1条回答
  •  执笔经年
    2021-01-22 18:28

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

    0 讨论(0)
提交回复
热议问题