linouk23 / youtube_uploader_selenium

Python script to upload videos on YouTube using Selenium
MIT License
612 stars 211 forks source link

a way to pass cookies headlessly? #8

Open ahmedtds opened 3 years ago

ahmedtds commented 3 years ago

need to pass first step headlessly, how to do it?

flazouh commented 3 years ago

Wondering too

gloliveira1701 commented 3 years ago

Did anyone get it?

tjtgzxs commented 3 years ago

Yes,You can set cookie file to execute firefox headlessly

itsjwala commented 3 years ago

@tjtgzxs @linouk23 can you please share how?

itsjwala commented 3 years ago

NVM, I changed the code a bit to sign in via automation, For Headless mode pass headless=True in the Firefox driver class

Constant.py

  YOUTUBE_SIGNIN_BUTTON = '//*[@id="buttons"]/ytd-button-renderer/a'
  GOOGLE_SIGNIN_CARD_EMAIL = '//*[@id="identifierId"]'
  GOOGLE_SIGNIN_CARD_EMAIL_NEXT = '//*[@id="identifierNext"]/div/button'
  GOOGLE_SIGNIN_CARD_PASSWORD = '//*[@id="password"]/div[1]/div/div[1]/input'
  GOOGLE_SIGNIN_CARD_PASSWORD_NEXT = '//*[@id="passwordNext"]/div/button'

__init__.py

    def __login(self):
        self.browser.get(Constant.YOUTUBE_URL)
        time.sleep(Constant.USER_WAITING_TIME)

        if self.browser.has_cookies_for_current_website():
            self.browser.load_cookies()
            time.sleep(Constant.USER_WAITING_TIME)
            self.browser.refresh()
        else:
            self.logger.debug('Couldnt find cookies. attempting login via automation')
            self.logger.debug('Clicking sign in button on top right corner')
            self.browser.find(By.XPATH, Constant.YOUTUBE_SIGNIN_BUTTON).click()
            time.sleep(Constant.USER_WAITING_TIME)
            self.logger.debug('Attempting to fill email')
            self.browser.find(By.XPATH,Constant.GOOGLE_SIGNIN_CARD_EMAIL).send_keys(os.getenv("YOUTUBE_USER_EMAIL"))
            time.sleep(Constant.USER_WAITING_TIME)
            self.logger.debug('Attempting to click next')
            self.browser.find(By.XPATH, Constant.GOOGLE_SIGNIN_CARD_EMAIL_NEXT).click()
            time.sleep(Constant.USER_WAITING_TIME)
            self.logger.debug('Attempting to fill password')
            self.browser.find(By.XPATH, Constant.GOOGLE_SIGNIN_CARD_PASSWORD ).send_keys(os.getenv("YOUTUBE_USER_PASS"))
            time.sleep(Constant.USER_WAITING_TIME)
            self.logger.debug('Attempting to go all in !')
            self.browser.find(By.XPATH, Constant.GOOGLE_SIGNIN_CARD_PASSWORD_NEXT).click()
            time.sleep(Constant.USER_WAITING_TIME)
            self.browser.save_cookies()

and passing youtube creds as environment variables.

In future Google might screw up the login form and XPATHs might need update

ghost commented 3 years ago

or do this self.browser = Firefox(current_working_dir, current_working_dir, headless=True) in here.