0

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);
azeros
  • 1
  • 1
  • Welcome to [Stack Overflow](https://stackoverflow.com/tour). Please check [How to Ask](https://stackoverflow.com/help/how-to-ask). It's also best to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to enable others to help you. That also shows what you have done so far and at which specific problem you are stuck. – MagnusO_O Nov 03 '22 at 19:26
  • See in particular my answer, which improves the accepted answer (in my opinion). – Booboo Nov 05 '22 at 10:53
  • @Booboo I'd had some trouble applying your answer to my script but got it working now, thank you! – azeros Nov 06 '22 at 22:06

0 Answers0