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

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

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

  1. 我该如何解决这个问题?
  2. 我怎样才能在普通的 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() 

回答

首先不要使用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)

这可以帮助您找到您的个人资料位置。

但是,为什么是 Firefox?

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

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

  • Doesn't work...

回答

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


以上是使用python和selenium自动化谷歌登录显示““此浏览器或应用程序可能不安全”“的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>