Home > Mobile >  Repeat program with different data
Repeat program with different data

Time:08-28

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()

CodePudding user response:

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

CodePudding user response:

The following would do the trick. Just create a list with all logins and passwords and the iterate over it with for login, password in logins:

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,
  Login2, Password2,
  Login3, Password3,
  Login4, Password4,
  Login5, Password5,
  Login6, Password6,
  Login7, Password7,
  Login8, Password8,
  Login9, Password9,
  Login10, Password10,
)
from Proxy_auth import Log1, Pass1, IP1
import time
import pickle

logins = [
  [Login1, Password1],
  [Login2, Password2],
  [Login3, Password3],
  [Login4, Password4],
  [Login5, Password5],
  [Login6, Password6],
  [Login7, Password7],
  [Login8, Password8],
  [Login9, Password9],
  [Login10, Password10],
]

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:
  for login, password in logins:
    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(login)
        time.sleep(2)
        login_input.send_keys(Keys.ENTER)
        time.sleep(4)
        password_input = driver.find_element(By.NAME, "password")
        password_input.send_keys(password)
        time.sleep(3)
        password_input.send_keys(Keys.ENTER)
        time.sleep(10)

        pickle.dump(driver.get_cookies(), open(f"{login}_cookies", "wb"))
        time.sleep(5)
    except Exception as ex:
        print(ex)
finally:
    driver.close()
    driver.quit()
  • Related