13

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
  • 151,581
  • 34
  • 225
  • 281
Ardo Rianda
  • 167
  • 1
  • 1
  • 6

8 Answers8

1

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
  • 130
  • 1
  • 3
  • 16
  • 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
1

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
  • 121
  • 3
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
  • 11,894
  • 5
  • 45
  • 44
0

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

-1

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
  • 6,852
  • 2
  • 16
  • 43
-3
  1. Open browser which controlled by selenium
  2. Open StackOverflow site manually(or another site which have authorization with google)
  3. Perform authorization with google manually

Then when you run your code again with gmail authorization it should work fine.

Tony
  • 6,852
  • 2
  • 16
  • 43
Norayr Sargsyan
  • 1,386
  • 1
  • 9
  • 23
-4

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
  • 149
  • 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
-4

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
  • 454
  • 5
  • 10