如何并行运行多个 selenium 驱动程序以使用文本文件输入(电子邮件:密码格式)登录 Instagram。
我想打开n个驱动程序,并对每个驱动程序并行执行,而不是一一执行。
我尝试过线程,但它只是停留在刷新相同的 Selenium 驱动程序上(也许我做错了)。
我应该使用线程还是多重处理?
我是新手,所以老实说我不知道该怎么做 这是我当前在一个驱动程序中执行此操作的代码片段
executable_path = "chromedriver.exe"
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=executable_path, options=options)
file = open("list.txt", "r")
for line in file:
pieces = line.split(":")
myemail = pieces[0]
mypass = pieces[1]
driver.get('https://www.instagram.com/accounts/login/')
time.sleep(5)
element = driver.find_elements_by_css_selector('form input')[0]
element.send_keys(myemail)
element1 = driver.find_elements_by_css_selector('form input')[1]
element1.send_keys(mypass)
最佳答案
建议使用python进行多处理,
下面是并行打开不同网址的示例
import multiprocessing as mp
def worker(url):
executable_path = "chromedriver.exe"
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=executable_path, options=options)
driver.get(url)
# do the necessary operations
# task1; task2; task3
# close instance after completed
driver.quit()
url_list = ["https://github.com/Aqua-4/auto-insta/blob/master/refresh_db.py","https://stackoverflow.com/questions/59706118/how-to-run-multiple-selenium-drivers-parallelly"]
if __name__ == '__main__':
p = mp.Pool(mp.cpu_count())
p.map(worker, url_list)
这将打开与您设备上的核心数量一样多的 selenium 实例,您也可以 mp.cpu_count() 更改此数字,但不建议将其增加到超过该数字设备上的核心数
关于python - 如何并行运行多个 Selenium 驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59706118/