Last active
November 9, 2024 00:14
-
-
Save jybaek/5f30fb6271716b19a5f1dd48937df51d to your computer and use it in GitHub Desktop.
Automatically recover Google account from GCE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
try: | |
from bs4 import BeautifulSoup | |
except ImportError: | |
print("Please install.") | |
print("pip install bs4.") | |
exit(255) | |
try: | |
from selenium import webdriver | |
except ImportError: | |
print("Please install.") | |
print("pip install selenium.") | |
exit(255) | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.common.by import By | |
from selenium.common.exceptions import TimeoutException | |
import sys | |
import argparse | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'--chromedriver', | |
type=str, | |
default='/usr/local/bin/chromedriver', | |
help='Location of chromedriver.') | |
parser.add_argument( | |
'--site', | |
type=str, | |
#default='https://example.kr', | |
default='https://accounts.google.com/signin/v2/recoveryidentifier?source=esir&flowName=GlifWebSignIn&flowEntry=ServiceLogin', | |
help='Your website address.') | |
parser.add_argument( | |
'--id', | |
type=str, | |
default='foo@example.com', | |
help='Your site ID.') | |
parser.add_argument( | |
'--passwd', | |
type=str, | |
default='passwd', | |
help='Your site password.') | |
FLAGS, unparsed = parser.parse_known_args() | |
options = webdriver.ChromeOptions() | |
# Headless option | |
options.add_argument('headless') | |
options.add_argument('window-size=1920x1080') | |
options.add_argument("disable-gpu") | |
browser = webdriver.Chrome(FLAGS.chromedriver, chrome_options=options) | |
browser.implicitly_wait(3) | |
browser.get(FLAGS.site) | |
title = WebDriverWait(browser, 10) \ | |
.until(EC.presence_of_element_located((By.CSS_SELECTOR, "head > title"))) | |
wait = WebDriverWait(browser, 20) | |
presence = EC.presence_of_element_located | |
visible = EC.visibility_of_element_located | |
html = browser.page_source | |
soup = BeautifulSoup(html, 'html.parser') | |
wait.until(presence((By.ID, 'identifierId'))) | |
browser.find_element_by_id('identifierId').send_keys(FLAGS.id) | |
browser.find_element_by_id("identifierNext").click() | |
wait.until(presence((By.ID, 'password'))) | |
browser.find_element_by_name('password').send_keys(FLAGS.passwd) | |
element = browser.find_element_by_id('passwordNext') | |
browser.execute_script("arguments[0].click();", element) | |
element = browser.find_element_by_id('continue_button') | |
browser.execute_script("arguments[0].click();", element) | |
browser.quit() | |
print("Browser parsing terminated.") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage