Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automation Google login with python and selenium shows ""This browser or app may be not secure""

I've tried login with Gmail or any Google services but it shows the following "This browser or app may not be secure" message:

error image

I also tried to do options like enable less secure app in my acc but it didn't work. then I made a new google account and it worked with me. but not with my old acc.

  1. how can i solve this ?
  2. How can i open selenium in the normal chrome browser not the one controlled by automated software ?

    This is my code
    from selenium.webdriver import Chrome
    from selenium.webdriver.chrome.options import Options


    browser = webdriver.Chrome()
    browser.get('https://accounts.google.com/servicelogin')
    search_form = browser.find_element_by_id("identifierId")
    search_form.send_keys('mygmail')
    nextButton = browser.find_elements_by_xpath('//*[@id ="identifierNext"]') 
    search_form.send_keys('password')
    nextButton[0].click() 
like image 850
Abdulrahman Dawoud Avatar asked Feb 15 '21 13:02

Abdulrahman Dawoud


People also ask

How do I bypass Google Captcha in Selenium?

While automating Captcha is not the best practice, there are three efficient ways of handling Captcha in Selenium: By disabling the Captcha in the testing environment. Adding a hook to click the Captcha checkbox. By adding a delay to the Webdriver and manually solve Captcha while testing.

Can you use Selenium with Gmail?

We can automate the Gmail login process using Selenium webdriver in Java. To perform this task, first we have to launch the Gmail login page and locate the email, password and other elements with the findElement method and then perform actions on them.

Why can't I sign into Google with selenium automation?

Unable to sign into google with selenium automation because of "This browser or app may not be secure." Ask Question Asked2 years, 1 month ago Active7 months ago Viewed27k times 14 4 I am trying to login to google with selenium and I keep getting the error that "This browser or app may not be secure."

Does allowing less secure apps on your Google account secure selenium?

3 I've seen other places that when people allow less secure apps it doesnt secure the problem. I tried it myself and it didnt work. Even though I allow it on my own google account it doesnt transfer to the window selenium opens.

Is it possible to use selenium with chrome and chromedriver?

#2 is impossible. Selenium needs chromedriver in order to control the browser. First of all don't use chrome and chromedriver. You need to use Firefox. (if not installed) Download and install Firefox. Login to Google with normal Firefox. You need to show the Google site that you are not a robot. You can use code like this:

Can selenium test scripts to login into Google account through Ajax?

Selenium test scripts to login into google account through new ajax login form Additional Considerations Finally, some old browser versions might not be supported, so ensure that: JDKis upgraded to current levels JDK 8u241. Seleniumis upgraded to current levels Version 3.141.59. ChromeDriveris updated to current ChromeDriver v80.0level.


Video Answer


4 Answers

This is working for me. I found the solution from GitHub.

   from selenium import webdriver
   from selenium_stealth import stealth

   options = webdriver.ChromeOptions()
   options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
   options.add_experimental_option("excludeSwitches", ["enable-automation"])
   options.add_experimental_option('useAutomationExtension', False)
   options.add_argument('--disable-blink-features=AutomationControlled')
   driver = webdriver.Chrome(options=options)
   stealth(driver,
        languages=["en-US", "en"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        )
   driver.get("https://www.google.com")
like image 87
Praveen Kumar Avatar answered Oct 28 '22 02:10

Praveen Kumar


First of all don't use chrome and chromedriver. You need to use Firefox.(if not installed) Download and install Firefox. Login to Google with normal Firefox.

You need to show the Google site that you are not a robot. You can use code like this:

from selenium import webdriver
import geckodriver_autoinstaller
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

geckodriver_autoinstaller.install()

profile = webdriver.FirefoxProfile(
    '/Users/<user name>/Library/Application Support/Firefox/Profiles/xxxxx.default-release')

profile.set_preference("dom.webdriver.enabled", False)
profile.set_preference('useAutomationExtension', False)
profile.update_preferences()
desired = DesiredCapabilities.FIREFOX

driver = webdriver.Firefox(firefox_profile=profile,
                           desired_capabilities=desired)

This can help you find your profile location.

But, why Firefox?

Actually there is only one reason, chromedriver was coded by Google. They can easily understand if it is a bot or not. But when we add user data with Firefox, they cannot understand if there is a bot or not.

You can fool Google like this. It worked for me too. I tried very hard to do this. Hope it will be resolved in you too.

like image 41
yusufusta Avatar answered Oct 28 '22 00:10

yusufusta


You can easily bypass the google bot detection with the undetected_chromedriver:

pip install undetected-chromedriver
pip install selenium

Credits: https://github.com/xtekky/google-login-bypass/blob/main/login.py

import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Main:
  def __init_(self) -> None:
    self.url    = 'https://accounts.google.com/ServiceLogin'
    self.driver = driver = uc.Chrome(use_subprocess=True)
    self.time   = 10
    
  def login(self, email, password):
    WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located((By.NAME, 'identifier'))).send_keys(f'{email}\n)
    WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located((By.NAME, 'password'))).send_keys(f'{password}\n)
                                                                                
    self.code()
                                                                                  
  def code(self):
    # [ ---------- paste your code here ---------- ]
    time.sleep(self.time)                                                                                  
                                                                                  
if __name__ == "__main__":
  #  ---------- EDIT ----------
  email = 'email' # replace email
  password = 'password' # replace password
  #  ---------- EDIT ----------                                                                                                                                                         
 
  driver = Main()
  driver.login(email, password) 
like image 4
xtekky Avatar answered Oct 28 '22 01:10

xtekky


From terminal pip install undetected-chromedriver then do the following steps, as shown below.
NOTE: indent your code inside if name == main, as i have done, only then the program will work

import undetected_chromedriver as uc
from time import sleep
from selenium.webdriver.common.by import By


if __name__ == '__main__':
    
    driver = uc.Chrome()
    driver.get('https://accounts.google.com/')

    # add email
    driver.find_element(By.XPATH, '//*[@id="identifierId"]').send_keys(YOUR EMAIL)
    driver.find_element(By.XPATH, '//*[@id="identifierNext"]/div/button/span').click()
    sleep(3)
    driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input').send_keys(YOUR PASSWORD)
    driver.find_element(By.XPATH, '//*[@id="passwordNext"]/div/button/span').click()
    sleep(10)


like image 1
Salim muneer lala Avatar answered Oct 28 '22 00:10

Salim muneer lala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!