I'm trying to make an Instagram BOT with selenium which can use multiple accounts simultaneously. So therefore, I needed to use threading. I have 2 accounts with the username and password saved in a text file. The problem is, I cannot login to both of the accounts simultaneously. What changes do I have to make in the code to do that?
Pardon me if there's any mistake in my explaination...
from selenium import webdriver
from time import sleep
import threading
def functions():
a = open('users.txt','r').readlines()
for i in a:
info = i.split()
driver = webdriver.Firefox()
driver.get('https://instagram.com')
sleep(5)
driver.find_element_by_name('username').send_keys(info[0])
driver.find_element_by_name('password').send_keys(info[1])
driver.find_element_by_class_name('sqdOP.L3NKy.y3zKF').click()
for _ in range(2):
t = threading.Thread(target=functions)
t.start()
A main problem of your code could be that each thread reads entire file and if you are a file with two rows, it will open four browser.
An other problem is that you before click on login button, you should close the cookie popup.
Pay attention that to enable the login button the password length must be minimum 6 characters.
Following can be an example of solution:
from selenium import webdriver
from time import sleep
from concurrent.futures import ThreadPoolExecutor
def functions(line):
info = line.split()
driver = webdriver.Firefox()
driver.get('https://instagram.com')
sleep(5)
driver.find_element_by_class_name("mt3GC").find_element_by_class_name("aOOlW.bIiDR").click() # close cookie
driver.find_element_by_name('username').send_keys(info[0])
driver.find_element_by_name('password').send_keys(info[1])
driver.find_element_by_class_name('sqdOP.L3NKy.y3zKF').click()
if __name__ == '__main__':
a = open('users.txt', 'r').readlines()
with ThreadPoolExecutor(max_workers=2) as executor:
for i in a:
future = executor.submit(functions, i)