Skip to content

Instantly share code, notes, and snippets.

@ikegami-yukino
Created June 12, 2015 09:26
Show Gist options
  • Save ikegami-yukino/51b247080976cb41fe93 to your computer and use it in GitHub Desktop.
Save ikegami-yukino/51b247080976cb41fe93 to your computer and use it in GitHub Desktop.
Automatically Google login by selenium
mail_address = ''
password = ''
from selenium import webdriver
UA = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0'
PHANTOMJS_ARG = {'phantomjs.page.settings.userAgent': UA}
driver = webdriver.PhantomJS(desired_capabilities=PHANTOMJS_ARG)
url = 'https://www.google.com/accounts/Login?hl=ja&continue=http://www.google.co.jp/'
driver.get(url)
driver.find_element_by_id("Email").send_keys(mail_address)
driver.find_element_by_id("next").click()
driver.find_element_by_id("Passwd").send_keys(password)
driver.find_element_by_id("signIn").click()
@moueenkhan
Copy link

@Mike-Fakesome @MarialenaShiamma @thiagobenine
I am getting this error. Any solution?

from session not created: This version of ChromeDriver only supports Chrome version 93
current browser version is 92.0.4515.107

I have installed the undetected chrome driver using pip install undetected-chromedriver command.
After installing, I am getting that error and I have checked but there is no version 93 available for Chrome.

@ALCarter2
Copy link

ALCarter2 commented Jul 30, 2021 via email

@andyb415
Copy link

@Mike-Fakesome - this is a life saver. after using the stack-overflow auth method for all of last year, i hadn't needed to run my particular automation since Dec 2020. that is until... yesterday. and it broke. i tried everything, but finally found this solution! and it works again.
in my case, i need to automate uploading previews (hundreds and hundreds) to doubleclick studio, which is owned by google. from there i can get individual urls with said creatives and get screen captures to look for line break and layout bugs. makes testing our DCO content so much faster!!!!
this helps so much. can't thank you enough.

@murilo3m
Copy link

undetected chromedriver version 2 works example:
`import undetected_chromedriver.v2 as uc
import random,time,os,sys
from selenium.webdriver.common.keys import Keys

GMAIL = '<GMAIL_HERE>'
PASSWORD = '<PASSWORD_HERE>'

chrome_options = uc.ChromeOptions()

chrome_options.add_argument("--disable-extensions")

chrome_options.add_argument("--disable-popup-blocking")

chrome_options.add_argument("--profile-directory=Default")

chrome_options.add_argument("--ignore-certificate-errors")

chrome_options.add_argument("--disable-plugins-discovery")

chrome_options.add_argument("--incognito")

chrome_options.add_argument("user_agent=DN")

driver = uc.Chrome(options=chrome_options)

driver.delete_all_cookies()

driver.get("https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow")

driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(GMAIL)
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(Keys.RETURN)

time.sleep(10)`

Worked for me today!

@rzfzr
Copy link

rzfzr commented Aug 30, 2021

Hey there people of the internet, I switched to a node app using the official API, however I noticed the API lacks many options (endscreens, cards...) present in tools such TubeBuddy, has anybody explored building a browser extension?

@smamczak
Copy link

smamczak commented Sep 8, 2021

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:49426
from session not created: This version of ChromeDriver only supports Chrome version 92
Current browser version is 94.0.4606.30

@banana-error
Copy link

banana-error commented Sep 21, 2021

@ochen1 please accept me, i am vulence# 0206 on discord. Thank you in advance!

@natsurkan
Copy link

@ochen1 please accept me too, paradoxx#3011

@Yhache
Copy link

Yhache commented Nov 1, 2021

Hi @ochen1 Can you please add me on Discord... i'd like to know your solution, i will explain why i need it, Thanks
Discord : yhache#8537

@furkanaydgn
Copy link

undetected chromedriver version 2 works example: `import undetected_chromedriver.v2 as uc import random,time,os,sys from selenium.webdriver.common.keys import Keys

GMAIL = '<GMAIL_HERE>' PASSWORD = '<PASSWORD_HERE>'

chrome_options = uc.ChromeOptions()

chrome_options.add_argument("--disable-extensions")

chrome_options.add_argument("--disable-popup-blocking")

chrome_options.add_argument("--profile-directory=Default")

chrome_options.add_argument("--ignore-certificate-errors")

chrome_options.add_argument("--disable-plugins-discovery")

chrome_options.add_argument("--incognito")

chrome_options.add_argument("user_agent=DN")

driver = uc.Chrome(options=chrome_options)

driver.delete_all_cookies()

driver.get("https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow")

driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(GMAIL) driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(Keys.RETURN)

time.sleep(10)`

Link is still blocking by google .Soo link should be update !!!

@furkanaydgn
Copy link

Hi @ochen1 Can you please add me on Discord... i'd like to know your solution, i will explain why i need it, Thanks Discord : yhache#8537

Did you find any solution ?

@Yhache
Copy link

Yhache commented Nov 22, 2021 via email

@kicktheken
Copy link

I've created a script to help people automate Google login here: https://github.com/crawlspec/google-login

It only needs the phone number for 2FA on first use. Subsequent uses will be 'trusted' by the service. It's written in puppeteer javascript. I can write a python selenium version if there's demand. Let me know if it works for you or post in the project's 'Issues' tab for any questions.

@Krzysiek191
Copy link

@kicktheken
I would appreciate a python selenium version.

@Fakesum
Copy link

Fakesum commented Feb 25, 2023

Current Best Method for google login is:

from seleniumbase import SB

with SB(uc=True) as driver:
    driver.get("https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow")
    driver.type("#identifierId", <Your Username Here>)
    driver.click("#identifierNext > div > button")

    driver.type("#password > div.aCsJod.oJeWuf > div > div.Xb9hP > input", <Password Here>)
    driver.click("#passwordNext > div > button")

    driver.save_screenshot("test.png")

I can't figure out how to make the window show though.

@X-Cotang
Copy link

X-Cotang commented Feb 25, 2023

Current Best Method for google login is:

from seleniumbase import SB

with SB(uc=True) as driver:
    driver.get("https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow")
    driver.type("#identifierId", <Your Username Here>)
    driver.click("#identifierNext > div > button")

    driver.type("#password > div.aCsJod.oJeWuf > div > div.Xb9hP > input", <Password Here>)
    driver.click("#passwordNext > div > button")

    driver.save_screenshot("test.png")

I can't figure out how to make the window show though.

it worked.

@Fakesum
Copy link

Fakesum commented Mar 23, 2023

Use This if you want head-full mode.

from seleniumbase import SB

with SB(uc=True, headed=True) as driver:
    driver.get("https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow")
    driver.type("#identifierId", <Your Username Here>)
    driver.click("#identifierNext > div > button")

    driver.type("#password > div.aCsJod.oJeWuf > div > div.Xb9hP > input", <Password Here>)
    driver.click("#passwordNext > div > button")

    driver.save_screenshot("test.png")

@362227
Copy link

362227 commented Aug 18, 2023

Use This if you want head-full mode.

from seleniumbase import SB

with SB(uc=True, headed=True) as driver:
    driver.get("https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow")
    driver.type("#identifierId", <Your Username Here>)
    driver.click("#identifierNext > div > button")

    driver.type("#password > div.aCsJod.oJeWuf > div > div.Xb9hP > input", <Password Here>)
    driver.click("#passwordNext > div > button")

    driver.save_screenshot("test.png")

This browser or app may not be secure.

@9velluru
Copy link

Requesting to provide some sample synthetic jobs that handle sign in page functionality. Please do the needful

@raihans8379
Copy link

raihans8379 commented Jan 16, 2024

@Fakesum Does this method only work locally? I am trying to standup some tests on jenkins but after entering the password this way, I'm then redirected to https://accounts.google.com/v3/signin/challenge/iap and cannot complete the auth flow.

works well locally though

EDIT: https://accounts.google.com/v3/signin/challenge/iap is a 2FA challenge for accounts that don't have 2FA enabled, to set it up in-line. Likely will need to forward proxy and that potentially won't help either.

@ozsoyhasan
Copy link

@Fakesum Does this method only work locally? I am trying to standup some tests on jenkins but after entering the password this way, I'm then redirected to https://accounts.google.com/v3/signin/challenge/iap and cannot complete the auth flow.

works well locally though

Same here.

@Fakesum
Copy link

Fakesum commented Mar 16, 2024

@Fakesum Does this method only work locally? I am trying to standup some tests on jenkins but after entering the password this way, I'm then redirected to https://accounts.google.com/v3/signin/challenge/iap and cannot complete the auth flow.

works well locally though

EDIT: https://accounts.google.com/v3/signin/challenge/iap is a 2FA challenge for accounts that don't have 2FA enabled, to set it up in-line. Likely will need to forward proxy and that potentially won't help either.

This works locally for me, it does give me a warning of suspicious activity but thats after logging in. try changing the url to "https://accounts.google.com/signin" since this will redirect to oauth v3 login screen instead of the old oauth v2 one.

@imbatman802
Copy link

@Fakesum Does this method only work locally? I am trying to standup some tests on jenkins but after entering the password this way, I'm then redirected to https://accounts.google.com/v3/signin/challenge/iap and cannot complete the auth flow.
works well locally though
EDIT: https://accounts.google.com/v3/signin/challenge/iap is a 2FA challenge for accounts that don't have 2FA enabled, to set it up in-line. Likely will need to forward proxy and that potentially won't help either.

This works locally for me, it does give me a warning of suspicious activity but thats after logging in. try changing the url to "https://accounts.google.com/signin" since this will redirect to oauth v3 login screen instead of the old oauth v2 one.

hey bro, can i contact you somewhere else? i really need your help

@dattv02
Copy link

dattv02 commented Apr 10, 2024

I wrote a javascript file to login. I made two tests:

  1. Open chrome via cmd and use chromedriver to connect to this existing chrome process, then run the javascript file in this chrome by selenium.
    Result: "This browser or app may not be secure"
  2. Open chrome via cmd, then paste content of javascript file into console to login,
    Result: Login successfully.
    What's the differences? I dont use any selenium functions like sendkeys, click, etc... All is done via the code in js file. The chrome processes in two cases opened via cmd not via selenium.

Hi bro. I 'am from VN. Can you help me automation login google

@Fakesum
Copy link

Fakesum commented Apr 27, 2024

its been more than a month but if you still, for whatever reason, need to contact me, you can do so on discord with the tag hello215

@Fakesum Does this method only work locally? I am trying to standup some tests on jenkins but after entering the password this way, I'm then redirected to https://accounts.google.com/v3/signin/challenge/iap and cannot complete the auth flow.
works well locally though
EDIT: https://accounts.google.com/v3/signin/challenge/iap is a 2FA challenge for accounts that don't have 2FA enabled, to set it up in-line. Likely will need to forward proxy and that potentially won't help either.

This works locally for me, it does give me a warning of suspicious activity but thats after logging in. try changing the url to "https://accounts.google.com/signin" since this will redirect to oauth v3 login screen instead of the old oauth v2 one.

hey bro, can i contact you somewhere else? i really need your help

Its been more than a month, but if you still need to contant me, you can do so on hello215 on discord

@dattv02
Copy link

dattv02 commented Apr 27, 2024

its been more than a month but if you still, for whatever reason, need to contact me, you can do so on discord with the tag hello215

@Fakesum Does this method only work locally? I am trying to standup some tests on jenkins but after entering the password this way, I'm then redirected to https://accounts.google.com/v3/signin/challenge/iap and cannot complete the auth flow.
works well locally though
EDIT: https://accounts.google.com/v3/signin/challenge/iap is a 2FA challenge for accounts that don't have 2FA enabled, to set it up in-line. Likely will need to forward proxy and that potentially won't help either.

This works locally for me, it does give me a warning of suspicious activity but thats after logging in. try changing the url to "https://accounts.google.com/signin" since this will redirect to oauth v3 login screen instead of the old oauth v2 one.

hey bro, can i contact you somewhere else? i really need your help

Its been more than a month, but if you still need to contant me, you can do so on hello215 on discord

Hi Guy. You can help me login google with python?
I have added your discord account. Thank bro

@Azedize
Copy link

Azedize commented Jul 24, 2024

from seleniumwire import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
import random

Paths and credentials

firefox_binary_path = r'C:\Program Files\Mozilla Firefox\firefox.exe'
profile_path = r'C:\Users\tec-d\AppData\Roaming\Mozilla\Firefox\Profiles\c6rj509q.default-release'
your_email = '@gmail.com'
your_password = ''

Proxy settings

proxies = [
"......",
]
proxy = random.choice(proxies)
proxy_host, proxy_port, proxy_username, proxy_password = proxy.split(':')

proxy_options = {
'proxy': {
'http': f'http://{proxy_username}:{proxy_password}@{proxy_host}:{proxy_port}',
'https': f'https://{proxy_username}:{proxy_password}@{proxy_host}:{proxy_port}',
'no_proxy': 'localhost,127.0.0.1'
}
}

Firefox options

FF_OPTIONS = [
'--no-sandbox',
'--start-maximized',
'--start-fullscreen',
'--single-process',
'--disable-dev-shm-usage',
'--incognito',
'--disable-blink-features=AutomationControlled',
'--disable-xss-auditor',
'--disable-web-security',
'--ignore-certificate-errors',
'--disable-notifications',
'--disable-infobars',
'--disable-extensions',
'--useAutomationExtension=False',
'--excludeSwitches=enable-automation',
]

Firefox profile preferences

SET_PREF = {
'general.useragent.override': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
'permissions.default.desktop-notification': 1,
'dom.webnotifications.enabled': 1,
'dom.push.enabled': 1,
'intl.accept_languages': 'en-US',
'network.proxy.type': 1,
'network.proxy.http': proxy_host,
'network.proxy.http_port': int(proxy_port),
'network.proxy.ssl': proxy_host,
'network.proxy.ssl_port': int(proxy_port),
'network.proxy.socks_username': proxy_username,
'network.proxy.socks_password': proxy_password,
'media.peerconnection.enabled': False,
'network.cookie.cookieBehavior': 0,
'network.cookie.lifetimePolicy': 0,
}

options = webdriver.FirefoxOptions()
options.binary_location = firefox_binary_path
options.add_argument(f'--profile={profile_path}')

for option in FF_OPTIONS:
options.add_argument(option)

browser = webdriver.Firefox(seleniumwire_options=proxy_options, options=options)

try:
browser.get('https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow')

email_field = WebDriverWait(browser, 40).until(EC.presence_of_element_located((By.ID, 'identifierId')))
email_field.send_keys(your_email)
email_field.send_keys(Keys.ENTER)

time.sleep(random.uniform(3, 6))

email_field = WebDriverWait(browser, 40).until(EC.presence_of_element_located((By.ID, 'identifierId')))
email_field.send_keys(your_email)
email_field.send_keys(Keys.ENTER)

time.sleep(random.uniform(3, 6))

password_field = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.NAME, 'password')))
password_field.send_keys(your_password)
password_field.send_keys(Keys.ENTER)

WebDriverWait(browser, 30).until(EC.title_contains('Inbox'))

except Exception as e:
print(f"An error occurred: {e}")

finally:
browser.quit() la meme probleme reste pour moi que conexion non securises mon besoin browser firefox

@MubasharNisar
Copy link

MubasharNisar commented Aug 2, 2024

I wrote a javascript file to login. I made two tests:

  1. Open chrome via cmd and use chromedriver to connect to this existing chrome process, then run the javascript file in this chrome by selenium.
    Result: "This browser or app may not be secure"
  2. Open chrome via cmd, then paste content of javascript file into console to login,
    Result: Login successfully.
    What's the differences? I dont use any selenium functions like sendkeys, click, etc... All is done via the code in js file. The chrome processes in two cases opened via cmd not via selenium.

Hi bro. I 'am from VN. Can you help me automation login google

@dattv02 I have managed to resolve this in Ruby, but I can help you implement a similar solution in Python. It works flawlessly to log in to a Google account on either the Chrome or Firefox browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment