4

As of today, a user cannot login to Google account using selenium in a new profile. I found that, Google is blocking the process(rejecting?) even on trying with stackauth. (Experienced this after updating to v90).

This is the answer that I'd posted previously for Google login using OAuth and that was working till very recently!
In short, you'll be logging in in-directly via stackauth.

  • The only way that I could do to bypass the restrictions is by disabling the Secure-App-Access or adding the below given argument.(Which I don't prefer as I cannot convince my users(100+) who use my app to disable that!)
    options.add_argument('user-data-dir=C:/Users/{username}/path to data of browser/')
  • The other sole way to login is by using stealth to fake the user agent to DN which is mentioned here and it works pretty good.
  • The major disadvantage that I found was you cannot open another tab when the automation is running, else, the process is interrupted. But this works perfectly with that disadvantage.
  • But the disadvantage that I found was, once if you login, you cannot get your job done, as the website that you're visiting restricts you and forces you to update the browser in order to access the website(Google Meet in my case). On the other hand, theoritically, one can open up the automation with the user data, but in the new window. And I feel its pretty optimal when compared to others except OAuth as it was the best way to do it.

Any other optimal working suggestions to bypass these restrictions by Google?

janw
  • 8,758
  • 11
  • 40
  • 62
theycallmepix
  • 474
  • 4
  • 13

2 Answers2

4

Finally, I was able to bypass Google security restrictions in Selenium successfully and hope it helps you as well. Sharing the entire code here.

In short:

  • You need to use old/outdated user-agent and revert back.

In detail:

  • Use selenium-stealth for faking the user agent.
  • Set user-agent to DN initially, before login.
  • Then, after logging in, revert back to normal.(not really, but chrome v>80) That's it.
    No need to keep the user data, enable less secure app access, nothing!

Here's the snippet of my code that currently works adn it's quite long tho!.(comments included for better understanding).

# Import required packages, modules etc.. Selenium is a must!

def login(username, password):       # Logs in the user
    driver.get("https://stackoverflow.com/users/login")
    WebDriverWait(driver, 60).until(expected_conditions.presence_of_element_located(
        (By.XPATH, '//*[@id="openid-buttons"]/button[1]'))).click()

    try:
        WebDriverWait(driver, 60).until(expected_conditions.presence_of_element_located(
            (By.ID, "Email"))).send_keys(username)      # Enters username
    except TimeoutException:
        del username
        driver.quit()
    WebDriverWait(driver, 60).until(expected_conditions.element_to_be_clickable(
        (By.XPATH, "/html/body/div/div[2]/div[2]/div[1]/form/div/div/input"))).click()      # Clicks NEXT
    time.sleep(0.5)

    try:
        try:
            WebDriverWait(driver, 60).until(expected_conditions.presence_of_element_located(
                (By.ID, "password"))).send_keys(password)       # Enters decoded Password
        except TimeoutException:
            driver.quit()
        WebDriverWait(driver, 5).until(expected_conditions.element_to_be_clickable(
            (By.ID, "submit"))).click()     # Clicks on Sign-in
    except TimeoutException or NoSuchElementException:
        print('\nUsername/Password seems to be incorrect, please re-check\nand Re-Run the program.')
        del username, password
        driver.quit()

    try:
        WebDriverWait(driver, 60).until(lambda webpage: "https://stackoverflow.com/" in webpage.current_url)
        print('\nLogin Successful!\n')
    except TimeoutException:
        print('\nUsername/Password seems to be incorrect, please re-check\nand Re-Run the program.')
        del username, password
        driver.quit()

USERNAME = input("User Name : ")
PASSWORD = white_password(prompt="Password  : ")      # A custom function for secured password input, explained at end.

# Expected and required arguments added here.
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('excludeSwitches', ['enable-logging'])

# Assign drivers here.

stealth(driver,
        user_agent='DN',
        languages=["en-US", "en"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        )       # Before Login, using stealth

login(USERNAME, PASSWORD)       # Call login function/method

stealth(driver,
        user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36',
        languages=["en-US", "en"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        )       # After logging in, revert back user agent to normal.

# Redirecting to Google Meet Web-Page
time.sleep(2)
driver.execute_script("window.open('https://the website that you wanto to go.')")
driver.switch_to.window(driver.window_handles[1])       # Redirecting to required from stackoverflow after logging in
driver.switch_to.window(driver.window_handles[0])       # This switches to stackoverflow website
driver.close()                                          # This closes the stackoverflow website
driver.switch_to.window(driver.window_handles[0])       # Focuses on present website

Click here learn about white_password.

theycallmepix
  • 474
  • 4
  • 13
3

Do this:

  1. Install this python module
pip install selenium-stealth
  1. Add this to your code:
from selenium_stealth import stealth

stealth(driver,
        languages=["en-US", "en"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        )

This worked for me.

janw
  • 8,758
  • 11
  • 40
  • 62
Havish.S
  • 815
  • 4
  • 9