问题
import threading
def rand_function1():
#random actions
def rand_function2():
#random actions
def main()
rand_function1
rand_function2
return
if __name__ == '__main__':
url_list = "https://www.rand_urls.com/"
driver = webdriver.Firefox()
for t in range(10):
t = threading.Thread(target=main)
t.start()
I have this simple program that I am trying to open urls using 10 Firefox web drivers. However, all it does it use one browser and continue to cycle though urls thought that individual browser. I will be using a unique proxies for each browser so opening tabs wont be an option.
How do I get n
threads to run the main function individually using its own Firefox web driver?
回答1:
According to this and this previous question, selenium is not thread safe.
You should create drivers inside your main, so that every thread has its own driver.
import threading
def rand_function1():
#random actions
def rand_function2():
#random actions
def main()
# use a different driver for each thread
driver = webdriver.Firefox()
rand_function1
rand_function2
return
if __name__ == '__main__':
url_list = "https://www.rand_urls.com/"
for t in range(10):
t = threading.Thread(target=main)
t.start()
来源:https://stackoverflow.com/questions/41716157/selenium-threads-how-to-open-the-same-browser-in-multiple-threads-for-the-purp