19

I want to login to my gmail account using selenium. I use python2.7 . It doesn't have error, but the page said that I couldn't sign in to my account because some reason. you can see the screenshot below. screenshot

it is my code:

import time
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

EXE_PATH = r'C:\Users\LENOVO\Downloads\geckodriver.exe'
driver = webdriver.Firefox(executable_path=EXE_PATH)

def login():
    mail = 'myMail'
    pw = 'myPassword'
    driver.get('https://gmail.com')
    email = driver.find_element_by_name('identifier')
    email.send_keys(mail)
    driver.find_element_by_id('identifierNext').click()
    time.sleep(10)
    password = driver.find_element_by_name('password')
    password.send_keys(pw)
    driver.find_element_by_id('passwordNext').click()

what have to I do? please help me, I just a noob and beginner. thanks master

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ardo Rianda
  • 327
  • 1
  • 3
  • 8

8 Answers8

9

First install undetected-chromedriver using pip. It's a library which bypass Chrome security and allow you to proceed further.

pip install undetected-chromedriver

Then instead of creating using chromedriver.exe like driver = webdriver.Chrome(r"chromedriver.exe"), use the Chrome() function from the library you just installed.

Full Code Example in Python:

import undetected_chromedriver.v2 as uc
from time import sleep

username = 'example@gmail.com'
password ='password'

driver = uc.Chrome()

driver.delete_all_cookies()

driver.get('https://accounts.google.com/ServiceLogin')
sleep(2)

driver.find_element_by_xpath('//input[@type="email"]').send_keys(username)
driver.find_element_by_xpath('//*[@id="identifierNext"]').click()
sleep(2)

driver.find_element_by_xpath('//input[@type="password"]').send_keys(password)
driver.find_element_by_xpath('//*[@id="passwordNext"]').click()
sleep(2)

driver.get('https://gmail.com')
sleep(2)
Rajan Gautam
  • 402
  • 4
  • 6
  • 2
    Im getting an error "AttributeError: 'Chrome' object has no attribute 'find_element_by_xpath'" Any idea what to do? – tlalco Jul 28 '22 at 12:53
  • 1
    @tlalco I think it maybe issue with version of `undetected_chromedriver` that you're using. Can you tell me which version you're using. You can find it using `pip freeze` command. – Rajan Gautam Jul 29 '22 at 17:09
  • I was able to solve it following this question. https://stackoverflow.com/questions/72754651/attributeerror-webdriver-object-has-no-attribute-find-element-by-xpath – tlalco Aug 01 '22 at 19:55
  • I got this error: "An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module: if __name__ == '__main__': freeze_support() ... The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable." – Alekk Aug 13 '22 at 21:15
  • 1
    I got the error when I did uc.Chrome() – Alekk Aug 13 '22 at 21:16
  • probably something todo with my python or selenium version – Alekk Aug 13 '22 at 21:28
  • is this package save? – titusfx Mar 25 '23 at 18:05
  • I had to `import undetected_chromedriver as uc` without "v2" to make it work. – Ghislain Viguier May 06 '23 at 13:06
3

The way to go around Googles automation detection, is by using the undetected_chromedriver library. You can install the package using pip install undetected-chromedriver. Once your driver object is initiated you can simply use selenium to operate the driver afterwards.

# initiate the driver with undetetcted_chromedriver
import undetected_chromedriver.v2 as uc
driver = uc.Chrome()

# operate the driver as you would with selenium
driver.get('https://my-url.com') 

# Example use of selenium imports to be used with the driver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

try:
    driver.find_element(By.XPATH, '//*[@id="my-id"]').click()
except NoSuchElementException:
    print("No Such Element Exception")

Note: the login of Google is a popup, so don't forget to swap window handles to the popup to login and then afterwards to switch back to the main window.

questioning
  • 245
  • 1
  • 4
  • 18
  • This doesn't work at all, and the package appears to be unmaintained. Even with their hacked driver, I still get the Google splash page. – Cerin Feb 21 '22 at 22:36
  • @Cerin I tested it again just now, and it still works for me. :-) – questioning Feb 22 '22 at 19:49
  • do they have the same for c#? seems like the do: [here](https://www.nuget.org/packages/Selenium.WebDriver.UndetectedChromeDriver) – manymanymore May 16 '23 at 10:29
2

This Google Support page states that sign in via browsers that "Use automation testing frameworks" is being disabled for the following security reasons and Google advices to do "Sign in with Google" using browser-based OAuth 2.0 authentication service.

As some websites, like stackoverflow.com allow you to sign in to their services using "Sign in with Google" it must happen via Google OAuth 2.0 authentication. This implicates that doing so you are also indirectly signing in to your Google account and therefore you can use all the Google services.

So you can fully automatically sign in to your Google account, e.g. by using a Python script, by performing these actions in your code:

  1. Open a new browser window that is controlled by selenium webdriver
  2. In the same window load the StackOverflow login page (or any other site that uses "Sign in with Google")
  3. Choose for "Log in with Google"
  4. Provide your Google account credentials and login to StackOverflow
  5. Load the Google mailbox by opening https://mail.google.com/ or https://www.gmail.com/

This way you land down in your Gmail mailbox without performing any manual actions.

Please remember to add some 5s delays between different actions as doing it too quickly or too frequently can be recognized by StackOverflow as malicious automated actions and you can get blocked and you will need to make the manual I'm not a robot verification

Tony
  • 7,767
  • 2
  • 22
  • 51
  • It works for me and I am using this approach every day to login in to my Gmail account ! – Tony Feb 05 '21 at 11:06
  • 1
    This no longer works. I guess google developers hang out on stackoverflow a lot :P – DollarAkshay Feb 15 '22 at 01:45
  • 1
    I guess google's position is that if you use Sign In With Google on your website, you're not allowed to use automated tests to test the sign in flow. Using a different sign in method than the users use kind of defeats the purpose of testing. – MattS May 12 '23 at 17:31
1

I haven't really solved this but there is a workaround. If just logging in is your sole purpose then this might not be useful for you but if you wanna perform further operations and logging in using google is getting in your way then:

  • You can manually login in your chrome browser.

  • Use your default chrome profile to launch chrome.

  • To find path to your chrome profile data you need to type chrome://version/ in your address bar .

      options = webdriver.ChromeOptions()
      options.add_argument("user-data-dir=<Path to your chrome profile>")
      chrome_driver_path = "C:/Users/..."
      driver = webdriver.Chrome(executable_path=chrome_driver_path, chrome_options=options)
    
  • This would skip your login steps

0

I can't login google with selenium or headless-chromium-php, but we can login google with chrome first, and find the "Profile Path" in "chrome://version/", for example my profile path is "/home/diyism/.config/google-chrome/Profile 2", then I copy it into "/home/diyism/.config/google-chrome/selenium-profiles/default", and run headless-chromium-php:

$factory = new \HeadlessChromium\BrowserFactory('/opt/google/chrome/chrome');
$browser = $factory->createBrowser([
        'headless' => false,
        'keepAlive' => true,
        'userDataDir'=>'/home/diyism/.config/google-chrome/selenium-profiles'
    ]);
$page = $browser->createPage();
$page->navigate('https://mail.google.com')->waitForNavigation();
$pageTitle = $page->evaluate('document.title')->getReturnValue();
sleep(20);
$page->screenshot()->saveToFile('gmail.inbox.list.png');

Now I can see my gmail inbox list in gmail.inbox.list.png.

diyism
  • 12,477
  • 5
  • 46
  • 46
-1

To make the sign-in work, simply go to google, click "manage this account" for the account you want to log-in via selenium, go to the security tab, scroll down till you see "Enhanced Safe Browsing for your account", and turn it off.

Alekk
  • 92
  • 9
-3

That seems to be a known problem with automated logins.

If you want to pursue that, you need to enable less secure apps in your gmail account.

https://myaccount.google.com/lesssecureapps

This setting may not be available for:

Accounts with 2-Step Verification enabled: Such accounts require an application-specific password for less secure apps access. G Suite users: This setting is hidden if your administrator has locked less secure app account access.

Paulo Alves
  • 164
  • 8
  • 13
    thanks for your solution master. I was tried your solution, I was enable it. but, it don't work. the problem is still not resolved – Ardo Rianda Dec 30 '19 at 11:34
-6

Try this solution it worked for me I can even log in my Gmail now with this solution. after setting up the user-agent attribute it worked like a miracle.

<webview src="https://mail.google.com/" useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko"></webview>
JHM16
  • 649
  • 8
  • 12