Selenium Threads: How to open the same browser in multiple threads? for the purposes of each browser using a unique proxy

醉酒当歌 提交于 2020-01-02 11:29:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!