0

I am currently making a script utilizing selenium. I would like multiple "tasks" to be running at the same time. How would I write code to have the same code running multiple times at once?

manatee
  • 19
  • 5

1 Answers1

0

You can use threading for this. Here's a short example:

from selenium import webdriver
import threading


def go_to_example(driver):
    driver.get('https://example.com')


drivers = []
for _ in range(3):
    drivers.append(
        webdriver.Chrome()
    )
threads = [threading.Thread(target=go_to_example, args=(d,)) for d in drivers]
for t in threads:
    t.start()
for t in threads:
    t.join()
daaawx
  • 3,273
  • 2
  • 17
  • 16
  • 1
    https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects – jizhihaoSAMA Nov 08 '20 at 13:24
  • @jizhihaoSAMA Thanks for sharing. I've updated the code to be more pythonic. – daaawx Nov 08 '20 at 13:33
  • Should I make my whole program into a function so it would work with threading? – manatee Nov 08 '20 at 14:35
  • It depends on what you're trying to do. Generally it's important to encapsulate the implementation to support threading. In this case your selenium functionality might go inside the `go_to_example` function, it doesn't have to be the whole program. – daaawx Nov 08 '20 at 14:55
  • Hmmmm. I'm getting this error: "TypeError: 'WebDriver' object is not iterable " – manatee Nov 08 '20 at 22:35
  • You are probably iterating over the webdriver element instead of the list of `drivers` somewhere in the code. – daaawx Nov 09 '20 at 17:31