使用 python 和 selenium 进行自动化 Google 登录显示“”此浏览器或应用程序可能不安全“”

Posted

技术标签:

【中文标题】使用 python 和 selenium 进行自动化 Google 登录显示“”此浏览器或应用程序可能不安全“”【英文标题】:Automation Google login with python and selenium shows ""This browser or app may be not secure"" 【发布时间】:2021-05-18 09:41:55 【问题描述】:

我尝试使用 Gmail 或任何 Google 服务登录,但它显示以下“此浏览器或应用程序可能不安全”消息:

我还尝试过在我的账户中启用安全性较低的应用等选项,但没有奏效。 然后我创建了一个新的谷歌帐户,它对我有用。但不是我的旧账户。

    我该如何解决这个问题? 如何在普通 chrome 浏览器中打开 selenium 而不是由自动化软件控制的浏览器 ? 这是我的代码
    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() 

【问题讨论】:

#2 是不可能的。 Selenium 需要 chromedriver 来控制浏览器。 【参考方案1】:

首先不要使用chrome和chromedriver。您需要使用 Firefox。(如果未安装)下载并安装 Firefox。使用普通 Firefox 登录 Google。

您需要向 Google 网站表明您不是机器人。你可以使用这样的代码:

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 可以帮助您找到您的个人资料位置。

但是,为什么是 Firefox?

其实只有一个原因,chromedriver 是谷歌编码的。 他们可以很容易地理解它是否是机器人。但是当我们用 Firefox 添加用户数据时,他们无法理解是否存在 bot。

你可以这样欺骗 Google。它也对我有用。我非常努力地做到这一点。希望它也能在你身上得到解决。

【讨论】:

考虑详细说明为什么带有 Chromedriver 的 Chrome 不起作用以及为什么 Firefox 方法起作用。 @MLarionov 显然它非常简单。 Chrome 由 Google 编码。他们可以很容易地理解它是否是机器人。但火狐不一样。当我们使用用户数据时,它看起来就像一个真正的浏览器。 (+1) 我刚刚添加了driver = webdriver.Firefox(firefox_profile=path),它成功了!我可以按照here 的描述找到我的个人资料路径。 它将于 2021 年在 mac 上运行! 我的配置文件在磁盘上大约 1GB,当您指定配置文件时,硒代码会在您创建驱动程序时复制整个配置文件。为了解决这个问题,为自动化创建一个新的空配置文件,然后用它打开一个浏览器并使用它通过谷歌登录进行身份验证,然后将 selenium 指向这个配置文件。【参考方案2】:

这对我有用。我从 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")

【讨论】:

在具有泰国 IP 的 macbook 上无法使用 对不起,我没有用 Mac OS 检查它。尝试更改UA、供应商、平台等选项。 在我的 macbook 上工作,使用印度 ip【参考方案3】:

您可以使用来自github 的 undetected-chromedriver

import undetected_chromedriver as uc
from selenium import webdriver

options = uc.ChromeOptions()
options.add_argument("--ignore-certificate-error")
options.add_argument("--ignore-ssl-errors")
# e.g. Chrome path in Mac =/Users/x/Library/xx/Chrome/Default/
options.add_argument( "--user-data-dir=<Your chrome profile>")
driver = uc.Chrome(options=options)
url='https://accounts.google.com/servicelogin'
driver.get(url)

您的第一次导入需要未被检测到的 Chrome 驱动程序。

【讨论】:

【参考方案4】:

Google 检测到您正在使用漫游器。您必须首先添加此配置的 sn-p(将其转换为 python,因为我是用 java 编写的):

    options.addArguments("--no-sandbox");
            options.addArguments("--disable-dev-shm-usage");
            options.addArguments("--disable-blink-features");
            options.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
            options.addArguments("--disable-blink-features=AutomationControlled");
            options.addArguments("--disable-infobars");

        options.addArguments("--remote-debugging-port=9222");

options.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);

driver.executeScript("Object.defineProperty(navigator, 'webdriver', get: () => undefined)");

最后一条指令是关键,当你启动 Selenium 时,Chrome 的 navigator var 设置为 'true',这意味着浏览器是由一个 bot 控制的,用 JS 将它设置为 undefined 使其呈现为“正常”浏览器”在谷歌看来。 你应该做的另一件重要的事情是更改 chromedriver 二进制文件的 HEX,如果你相信我 obv,我可以将我已经修改过的一个(ubuntu 环境)发给你。

【讨论】:

嗨 Edoardo,你能把修改后的 chromedriver 二进制文件发给我吗?【参考方案5】:

如果您更喜欢 Chrome 而不是 Firefox,绕过 Google 自动检测的方法是使用 undetected_chromedriver 库。 您可以使用pip install undetected-chromedriver 安装该软件包。 启动驱动程序对象后,您可以简单地使用 selenium 来操作驱动程序。

# 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")

注意:谷歌的登录是一个弹出窗口,所以不要忘记将窗口句柄交换到弹出窗口进行登录,然后再切换回主窗口。

【讨论】:

以上是关于使用 python 和 selenium 进行自动化 Google 登录显示“”此浏览器或应用程序可能不安全“”的主要内容,如果未能解决你的问题,请参考以下文章

使用 python 和 selenium 进行自动化 Google 登录显示“”此浏览器或应用程序可能不安全“”

python系列35:使用selenium进行自动化网页操作

自动化测试常用断言的使用方法(python+selenium)

爬虫?还是自动化?Python使用Selenium自动进行百度搜索

爬虫?还是自动化?Python使用Selenium自动进行百度搜索

爬虫?还是自动化?Python使用Selenium自动进行百度搜索