how do i keep myself logged in heroku (chromedriver) even after dyno restarts everyday?

385 views Asked by At

I have made a bot in selenium-python and running it through Heroku , the bot logs in everyday into Instagram and does it activity, now the problem here is that Heroku -dynos restart everyday ..so the bot has to login everyday into Instagram. I want to save my login session in ChromeDriver so that even if the Dynos restart then the bot does not need to login ..how do i do this?

here is the code i am using to declare driver

class Account:
    def __init__(self,username,password,dm,comments,user):
        self.user=user
        self.username=username
        self.password=password
        options = webdriver.ChromeOptions()
        options.add_argument('--no-sandbox')
        options.add_argument("--disable-dev-shm-usage")
        options.add_argument("--disable-gpu")
        try:
            driver = webdriver.Chrome(options=options)
            self.driver=driver
        except:
            traceback.print_exc()
        self.followedtoday=[]
        self.startofcode=time.time()
        self.resetvars()
        self.commentarray=commentarray
        self.searcharray=searcharray
        self.message=dm
        self.comments=comments
        self.visit=[]
    self.hours=0

def login(self):
        self.driver.get('https://www.instagram.com/')
        print('logging in account of ',self.username)
        try:
            #9 seconds to load page
            wait()
            wait()
            print (self.driver.page_source.encode("utf-8"))
            username = self.driver.find_element_by_xpath("//input[@aria-label='{}']".format('Phone number, username, or email'))                                                    
            username.send_keys(self.username)
            password = self.driver.find_element_by_xpath("//input[@aria-label='{}']".format('Password'))                                                       
            password.send_keys(self.password)
            #wait to type
            wait()
            wait()
            sign_in_button = self.driver.find_element_by_xpath("//*[text()='{}']".format('Log In'))
            sign_in_button.click()
            wait()
1

There are 1 answers

2
Shahrear Bin Amin On BEST ANSWER

From Chrome capabilities documentations

Use custom profile (also called user data directory)
By default, ChromeDriver will create a new temporary profile for each session.

Therefor you need to create and load a custom profile by user-data-dir.

ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");

As a basic, but of course to be adapted to use implementation as you like. You can check loaded profile by browsing to chrome://version

It should also be OK use the options.add_argument('--profile-directory=<profile>') to load the profile.