17

I am trying to login to google with selenium and I keep getting the error that "This browser or app may not be secure."

The function I use to log in is:

async function loginToChrome(driver, username, password) {
  await driver.get("https://accounts.google.com/signin");
  await driver.sleep(1000);
  let email_phone = await driver.findElement(
    By.xpath("//input[@id='identifierId']")
  );
  await email_phone.sendKeys(username);
  await driver.findElement(By.id("identifierNext")).click();
  await driver.sleep(1000);

  let passEl = await driver.findElement(By.xpath("//input[@name='password']"));
  await passEl.sendKeys(password);
  await driver.findElement(By.id("passwordNext")).click();
  await driver.sleep(1000);
}

It is the same problem as https://stackoverflow.com/questions/59433453/unable-to-log-into-google-account-in-selenium-chrome-driver and https://stackoverflow.com/questions/59276975/couldnt-sign-you-in-this-browser-or-app-may-be-insecure-python-selenium-chrome

I have tried using both the chrome and firefox web drivers and both don't work. I have also tried doing .excludeSwitches(['enable-automation']) which also didn't help.

This made me think that maybe the sign-in page could detect that I was running in an automated environment. I tried this solution that would hide that the app is running in a web driver: Can a website detect when you are using selenium with chromedriver?

I have also looked into the User-Agent to see if that was the problem but what I have found is that it is identical to my regular chrome one.

All of this has not worked which makes leaves me stuck. I have seen solutions that say to use an already created user profile from your normal installation of chrome, but this wouldn't work for my use case.

Has anyone found the solution to this? I have been searching for hours and have come up empty-handed.

EDIT: It seems like this has been getting a lot of attention recently. I found a solution that allowed me to continue to use an automated client without having too many problems. Switching to Puppeteer. Look into these packages:

    "puppeteer",
    "puppeteer-extra",
    "puppeteer-extra-plugin-stealth"

EDIT 2: I have seen this get a lot of attention recently. I found the code that I ended up using to login. I used puppeteer instead of selenium to do this

async function login(
  page: Page,
  username: string,
  password: string,
  backup: string
) {
  await page.goto("https://accounts.google.com/");

  await page.waitForNavigation();

  await page.waitForSelector('input[type="email"]');
  await page.click('input[type="email"]');

  await page.waitForNavigation();

  //TODO : change to your email
  await page.type('input[type="email"]', username);
  await page.waitForSelector("#identifierNext");
  await page.click("#identifierNext");

  await page.waitFor(1000);

  await page.waitForSelector('input[type="password"]');
  await page.click('input[type="password"]');
  await page.waitFor(500);
  //TODO : change to your password
  await page.type('input[type="password"]', password);

  await page.waitForSelector("#passwordNext");
  await page.click("#passwordNext");
  await page.waitForNavigation();
}
KolCrooks
  • 514
  • 1
  • 5
  • 12

6 Answers6

9

The followings work me as well: 1. try to login stackoverflow with your google account 2. once login, go to the email

here is solution

     WebDriver driver;
    System.setProperty("webdriver.chrome.driver", "chromeDriver/chromedriver.exe");
    driver = new ChromeDriver();
     GeneralClass te =  new GeneralClass ();

    driver.get("https://accounts.google.com/signin/oauth/identifier?client_id=717762328687-iludtf96g1hinl76e4lc1b9a82g457nn."
            + "apps.googleusercontent.com&as=JS6BM8cjL-8j9votansdkw&destination=https%3A%2F%2Fstackauth"
            + ".com&approval_state=!ChRoYWVvLUlNMk5hSXJWUGlaSVl2WBIfc3lSa0lueENpb29lSU5vbEVpbVNxcUZGaGNkSEJoYw%E2%88%99AJDr988AAAAAXlBKc7PzEomxSzgNqd4wLptVlf0Ny3Qx&oauthgdpr=1&xsrfsig=ChkAeAh8T8JNDxCf2Zah5fb_rQ55OMiF8KmMEg5hcHByb3ZhbF9zdGF0ZRILZGVzdGluYXRpb24SBXNvYWN1Eg9vYXV0aHJpc2t5c2NvcGU&flowName=GeneralOAuthFlow");
     te.waitingForElementSendingKey(driver, By.id("identifierId"), "XXXXXXXX@gmail.com");
     te.waitingForElementForClickOnly(driver, By.id("identifierNext"));
     te.waitingForElementSendingKey(driver,By.name("password"), "PASSSWORD");
     te.waitingForElementForClickOnly(driver, By.id("passwordNext"));
     Thread.sleep(1500);
     driver.get("https://mail.google.com/mail/u/0/#inbox");

Thanks

B Banik
  • 154
  • 3
5

I just tried something out that worked for me after several hours of trial and error.

Adding args: ['--disable-web-security', '--user-data-dir', '--allow-running-insecure-content' ] to my config resolved the issue.

I realized later that this was not what helped me out as I tried with a different email and it didn't work. After some observations, I figured something else out and this has been tried and tested.

Using automation:

Go to https://stackoverflow.com/users/login Select Log in with Google Strategy Enter Google username and password Login to Stackoverflow Go to https://gmail.com (or whatever Google app you want to access)

After doing this consistently for like a whole day (about 24 hours), try automating your login directly to gmail (or whatever Google app you want to access) directly... I've had at least two other people do this with success. PS - You might want to continue with the stackoverflow login until you at least get a captcha request as we all went through that phase as well.

Yinka Alabi
  • 307
  • 2
  • 3
  • Thank you very much. Those config solved my issue! My issue is that in my login screen, there's a Login button via Gmail, everything works properly with Normal Chrome, but cannot login with Headless chrome. Adding `--disable-web-security` and `--allow-running-insecure-content` solved the issue! – Linh Nhat Nguyen Jul 17 '20 at 03:25
  • 1
    @LinhNhatNguyen although those were what I tried first to resolve my issue and it didn't actually resolve it (something else did which was the update I provided using automation), I'm glad leaving that there helped you with yours. – Yinka Alabi Jul 18 '20 at 07:00
1

One workaround that worked for me is creating a google account in the chrome instance started by the webdriver. Using this newly created account works for me, but I cannot tell what is exactly the difference between it and other google accounts.

1

Here's what worked for me:

I am using Puppeteer, but I'd bet it's the same for any automated scripts.

You must have a userDataDirectory so that the browser can use the same storage information.

You must initially run the script with headless: false so that you can get a browser to open. If you try to sign in on the current tab (the tab that was navigated automatically), then you will get that error on every sign-in attempt.

The trick (for me) was to open a new tab, navigate manually, try again.

Next time you run the script, you do not need to login.

bozdoz
  • 12,550
  • 7
  • 67
  • 96
  • 1
    Sorry, this trick doesn't work anymore for about a week now (see my question https://stackoverflow.com/questions/63773283/google-login-fails-with-http-error-400-saying-sorry-something-went-wrong-there/63773378). Any other idea? – fuggi Sep 07 '20 at 11:16
-1

Try using undetected_chromedriver library :


!pip install undetected_chromedriver

import undetected_chromedriver as uc

driver = uc.Chrome(executable_path='chromedriver.exe') #change for your path

driver.get('https://accounts.google.com/ServiceLogin') 
#continue work code here...

It worked pretty well for me

Nick Vu
  • 14,512
  • 4
  • 21
  • 31
DaniloM
  • 1
  • 1
-3

This error message...

This browser or app may not be secure.
Try using a different browser. If you’re already using a supported browser, you can refresh your screen and try again to sign in.

...implies that the WebDriver was unable to authenticate the Browsing Context i.e. Browser session.


Potential reasons and solution

There can be diverse reason behind this error as follows:

Solution

In these cases the respective solution would be to:

You can find a detailed discussion in Sign in to gmail account fails (selenium automation)


tl; dr

A couple of relevent documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352