Search code examples
pythonseleniumwebdriver

Repeat program with different data


Is it possible to automatically repeat this program 10 times? Only each time changing Login1, Password1 to Login2, Password2, next time Login2, Password2 to Login3, Password3 and so on.

from seleniumwire import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from Twitter_auth import Login1, Password1
from Proxy_auth import Log1, Pass1, IP1
import time
import pickle

capa = DesiredCapabilities.CHROME
capa["pageLoadStrategy"] = "none"
options = webdriver.ChromeOptions()
proxy = f"http://{Log1}:{Pass1}@{IP1}"
proxy_options = {
    'proxy': {
        'http': proxy,
        'https': proxy
    }
}
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options, seleniumwire_options=proxy_options)
options.add_argument("--disable-blink-features=AutomationControlled")
try:
    driver.page_load_strategy = 'none'
    driver.get(url="https://twitter.com/i/flow/login")
    time.sleep(4)
    login_input = driver.find_element(By.NAME, "text")
    login_input.send_keys(Login1)
    time.sleep(2)
    login_input.send_keys(Keys.ENTER)
    time.sleep(4)
    password_input = driver.find_element(By.NAME, "password")
    password_input.send_keys(Password1)
    time.sleep(3)
    password_input.send_keys(Keys.ENTER)
    time.sleep(10)

    pickle.dump(driver.get_cookies(), open(f"{Login1}_cookies", "wb"))
    time.sleep(5)



except Exception as ex:
    print(ex)
finally:
    driver.close()
    driver.quit()

Solution

  • Define a dictionary that contains login and password data

    Login = {'Login1': 'Password1',
             'Login2': 'Password2',
             'Login3': 'Password3',
             'Login4': 'Password4',
             'Login5': 'Password5',
             'Login6': 'Password6',
             'Login7': 'Password7',
             'Login8': 'Password8',
             'Login9': 'Password9',
             'Login10': 'Password10',
            }
    

    Use for-loop like this:

    for login, password in Login.items():
        
       # Use login and password instead of login1 and password1