Selenium Google Login Block

房东的猫 提交于 2020-03-01 06:35:37

问题


I have a problem with Google login. I want to login to the my account but Google says that automation drivers are not allowed to login.

I am looking for a solution. Is it possible to get cookie of normal Firefox/Chrome and load it into the ChromeDriver/GeckoDriver? I thought that this can be a solution. But I am not sure is it possible or not..

Looking for solutions..

Also, I want to add a quick solution for this. I solved this issue by using one of my old verified account. That can be a quick solution for you also.


回答1:


This error message...

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


This browser or app may not be secure

This error can happen due to different factors as follows:

  • In the article "This browser or app may not be secure" error when trying to sign in with Google on desktop apps @Raphael Schaad mentioned that, if an user can log into the same app just fine with other Google accounts, then the problem must be with the particular account. In majority of the cases the possible reason is, this particular user account is configured with Two Factor Authentification.

  • In the article Less secure apps & your Google Account it is mentioned that, if an app or site doesn’t meet google-chrome's security standards, Google may block anyone who’s trying to sign in to your account from it. Less secure apps can make it easier for hackers to get in to your account, so blocking sign-ins from these apps helps keep your account safe.


Solution

In these cases the respective solution would be to:

  • Disable Two Factor Authentification for this Google account and execute your @Test.
  • Allow less secure apps

You can find a detailed discussion in Unable to sign into google with selenium automation because of "This browser or app may not be secure."


Deep Dive

However, to help protect your account, Web Browsers may not let you sign in from some browsers. Google might stop sign-ins from browsers that:

  • Doesn't support JavaScript or have Javascript turned off.
  • Have AutomationExtension or unsecure or unsupported extensions added.
  • Use automation testing frameworks.
  • Are embedded in a different application.

Solution

In these cases there are diverse solutions:

  • Use a browser that supports JavaScript:

    • Chrome
    • Safari
    • Firefox
    • Opera
    • Internet Explorer
    • Edge
  • Turn on JavaScript in Web Browsers: If you’re using a supported browser and still can’t sign in, you might need to turn on JavaScript.

  • If you still can’t sign in, it might be because you have AutomationExtension / unsecure / unsupported extensions turned on and you may need to turn off as follows:

    public class browserAppDemo 
    {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.setExperimentalOption("useAutomationExtension", false);
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            WebDriver driver =  new ChromeDriver(options); 
            driver.get("https://accounts.google.com/signin")
            new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='identifierId']"))).sendKeys("gashu");
            driver.findElement(By.id("identifierNext")).click();
            new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='password']"))).sendKeys("gashu");
            driver.findElement(By.id("passwordNext")).click();
            System.out.println(driver.getTitle());
        }
    }
    
  • You can find a couple of relevant discussions in:

    • Gmail login using selenium webdriver in java
    • 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:

  • JDK is upgraded to current levels JDK 8u241.
  • Selenium is upgraded to current levels Version 3.141.59.
  • ChromeDriver is updated to current ChromeDriver v80.0 level.
  • Chrome is updated to current Chrome Version 79.0 level. (as per ChromeDriver v80.0 release notes)



回答2:


One Solution that works for me: https://stackoverflow.com/a/60328992/12939291 or https://www.youtube.com/watch?v=HkgDRRWrZKg

Short: Stackoverflow Login with Google Account with Redirect

from selenium import webdriver
from time import sleep

class Google:

    def __init__(self, username, password):

        self.driver = webdriver.Chrome('./chromedriver')
        self.driver.get('https://stackoverflow.com/users/signup?ssrc=head&returnurl=%2fusers%2fstory%2fcurrent%27')
        sleep(3)
        self.driver.find_element_by_xpath('//*[@id="openid-buttons"]/button[1]').click()
        self.driver.find_element_by_xpath('//input[@type="email"]').send_keys(username)
        self.driver.find_element_by_xpath('//*[@id="identifierNext"]').click()
        sleep(3)
        self.driver.find_element_by_xpath('//input[@type="password"]').send_keys(password)
        self.driver.find_element_by_xpath('//*[@id="passwordNext"]').click()
        sleep(2)
        self.driver.get('https://youtube.com')
        sleep(5)

username = ''
passwort = ''
Google(username, password)


来源:https://stackoverflow.com/questions/60117232/selenium-google-login-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!