I'm wanting to write a script in python, with selenium, to automate logging into a website and performing certain tasks on multiple accounts in parallel.
I'm taking the logins from a text file with the format username:password:2fa
In my current attempt, I've succeeded in doing this with a single account and on all accounts one after the other. Eg, logging in with account 1, performing tasks, and then moving on to account 2, etc, but I want to be able to do this with each account at the same time as their tasks would be identical.
I've tried using both multithreading and multiprocessing to no avail but am unsure which would be best suited to this script.
Here is my current code:
def worker(url):
executable_path = "chromedriver.exe"
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 30)
driver.get(url)
wait.until(expected_conditions.element_to_be_clickable((By.ID, "identifierId")));
driver.find_element(By.ID, "identifierId").send_keys(userName)
driver.find_element(By.CSS_SELECTOR, ".VfPpkd-LgbsSe-OWXEXe-k8QpJ > .VfPpkd-vQzf8d").click()
wait.until(expected_conditions.element_to_be_clickable((By.NAME, "passwd")));
driver.find_element(By.NAME, "Passwd").send_keys(password)
driver.find_element(By.CSS_SELECTOR, ".VfPpkd-LgbsSe-OWXEXe-k8QpJ > .VfPpkd-vQzf8d").click()
#login to account
#do task1, task2, task3 etc.
#log out and move to next account
time.sleep(5)
print(driver.title)
driver.quit()
url = 'https://accounts.google.com/signin'
if __name__ == '__main__':
with open("logins.txt") as f:
for i, line in enumerate(f):
line = line.strip()
if len(line) > 0:
data, _ = line.split('#')
userName, password, code = data.split(':')
userName = userName.strip()
password = password.strip()
code = code.strip()
worker(url);