Skip to content

Instantly share code, notes, and snippets.

@ikegami-yukino
Created June 12, 2015 09:26
Show Gist options
  • Star 55 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • 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()
@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

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