0

我在 selenium-python 中制作了一个机器人并通过 Heroku 运行它,该机器人每天登录 Instagram 并进行活动,现在这里的问题是 Heroku -dynos 每天都会重新启动..所以机器人必须每天登录 Instagram。 我想在 ChromeDriver 中保存我的登录会话,这样即使 Dynos 重新启动,机器人也不需要登录..我该怎么做?

这是我用来声明驱动程序的代码

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

1 回答 1

1

来自 Chrome 功能文档

使用自定义配置文件(也称为用户数据目录)
默认情况下,ChromeDriver 将为每个会话创建一个新的临时配置文件。

因此,您需要创建并加载自定义配置文件user-data-dir

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

作为一个基本的,但当然要适应使用你喜欢的实现。您可以通过浏览来检查加载的配置文件chrome://version

options.add_argument('--profile-directory=<profile>')使用加载配置文件也应该没问题。

于 2020-12-15T14:44:14.363 回答