硒谷歌登录块

Ahm*_*şli 29 selenium google-chrome selenium-chromedriver selenium-webdriver geckodriver

我在使用 Google 登录时遇到问题。我想登录我的帐户,但 Google 说不允许自动化驱动程序登录。

我正在寻找解决方案。是否可以获取普通 Firefox/Chrome 的 cookie 并将其加载到 ChromeDriver/GeckoDriver 中?我认为这可以是一个解决方案。但我不确定这是否可能..

正在寻找解决方案...

在此处输入图片说明

另外,我想添加一个快速解决方案。我通过使用我的旧验证帐户之一解决了这个问题。这对你来说是一个快速的解决方案。

Duk*_*uke 10

我遇到了同样的问题并找到了解决方案。我在用

1) 视窗 10 专业版

2) 铬版 83.0.4103.97 (Official Build) (64-bit)

3)硒ChromeDriver 83.0.4103.39

一些打开谷歌页面的简单 C# 代码

var options = new ChromeOptions();
options.addArguments(@"user-data-dir=c:\Users\{username}\AppData\Local\Google\Chrome\User Data\");
IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver();
driver = new ChromeDriver(Directory.GetCurrentDirectory(), options);
driver.Url = "https://accounts.google.com/";
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud)

这里的核心问题是你使用selenium驱动时无法登录,但是你可以使用已经登录到谷歌账户的配置文件

您必须找到您的 Chrome 商店配置文件所在的位置并将其附加到"user-data-dir"选项中。

附注。将 {username} 替换为您的真实帐户名。

在 linux 上,用户配置文件位于"~/.config/google-chrome".

  • 有没有办法在 Mac 上做到这一点 (2认同)
  • 我有所有这些选项: - chrome_options.add_argument("--disable-web-security") - chrome_options.add_argument("--allow-running-insecure-content") - chrome_options.add_argument("--user-data -dir=/Users/matt/Library/Application Support/Google/Chrome" ) - chrome_options.add_argument('--profile-directory=Default') 但它仍然对我不起作用。它仍然说浏览器不安全。我使用的是 Mac OS 和 Chrome 版本 93。 (2认同)

Deb*_*anB 8

这个错误信息...

\n

此浏览器或应用程序可能不安全

\n

...意味着WebDriver实例无法验证浏览上下文(即浏览器)会话)。

\n
\n

此浏览器或应用程序可能不安全

\n

此错误可能由于以下不同因素而发生:

\n\n
\n

解决方案

\n

在这些情况下,相应的解决方案是:

\n\n
\n

您可以在由于“此浏览器或应用程序可能不安全”而无法使用 selenium 自动化登录 google 中找到详细讨论。

\n
\n
\n

深潜

\n

但是,为了帮助保护您的帐户,网络浏览器可能不允许您从某些浏览器登录。Google 可能会停止通过以下浏览器登录:

\n
    \n
  • 不支持 JavaScript 或已关闭 JavaScript。
  • \n
  • 添加了 AutomationExtension 或不安全或不受支持的扩展。
  • \n
  • 使用自动化测试框架。
  • \n
  • 嵌入到不同的应用程序中。
  • \n
\n
\n

解决方案

\n

在这些情况下,有多种解决方案:

\n
    \n
  • 使用支持JavaScript的浏览器

    \n
  • \n
  • 铬合金

    \n
  • \n
  • 苹果浏览器

    \n
  • \n
  • 火狐浏览器

    \n
  • \n
  • 歌剧

    \n
  • \n
  • IE浏览器

    \n
  • \n
  • 边缘

    \n
  • \n
  • 在Web 浏览器中打开 JavaScript:如果您\xe2\x80\x99 使用受支持的浏览器,并且仍然无法\xe2\x80\x99 登录,则可能需要打开 JavaScript。

    \n
  • \n
  • 如果您仍然可以\xe2\x80\x99t 登录,可能是因为您的AutomationExtension /不安全/不受支持扩展,并且您可能需要按以下方式关闭:

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

其他注意事项

\n

最后,某些旧的浏览器版本可能不受支持,因此请确保:

\n\n

  • 我在其他地方看到,当人们允许安全性较低的应用程序时,它并不能解决问题。我自己尝试了一下,没有成功。即使我在我自己的谷歌帐户上允许它,它也不会转移到硒打开的窗口。如果有人需要访问 Gmail 帐户或使用 google 服务,您对浏览器自动化方法还有其他建议吗……现在使用 selenium 并不容易/可能做到这一点 (3认同)

Tim*_*ayy 6

无需重定向、使用 Firefox 驱动程序或更改任何 google 帐户设置的解决方案:

如果您有要访问的特定 Google 帐户,请使用它创建 chrome 配置文件,然后在使用 selenium 时加载 chrome 配置文件:

options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=C:/Users/{userName}/AppData/Local/Google/Chrome/User Data/Profile {#}/")
driver = webdriver.Chrome("C:/bin/chromedriver.exe", chrome_options=options)
Run Code Online (Sandbox Code Playgroud)

Windows:
上面文件路径中的配置文件 {#} 会有所不同,因此我建议检查用户数据文件夹内您要使用的配置文件。例如,如果您当前只有 1 个 chrome 帐户,则用户数据中将没有配置文件目录(诉诸“默认”目录),但如果您创建第二个 chrome 帐户,则用户数据中将有一个“配置文件 1”目录。

请注意,您应该创建一个新的 google chrome 配置文件以与 selenium 一起使用,因为尝试使用已在使用的 chrome 配置文件(在另一个 chrome 窗口中打开)将导致错误。

Mac:此解决方案在 Mac 上可能有效,也可能无效,但要找到 chrome 帐户文件夹/文件路径,请按照@bfhaha
留下的评论中的说明进行操作

  • @cadd_coder 这个答案对我不起作用。但我知道个人资料在哪里。访问`chrome://version/`有一个`Profile Path`。它告诉您个人资料在哪里。在我的 MacOS 中,它是“/Users/bfhaha/Library/Application Support/Google/Chrome/Default”。请注意,您无法在 Finder 中看到 Library 文件夹。默认情况下它是隐藏的。因此只需使用[前往]和[前往文件夹]即可浏览文件夹。 (3认同)
  • 如何在 Mac Book 上做到这一点? (2认同)

小智 5

一种适合我的解决方案:https : //stackoverflow.com/a/60328992/12939291https://www.youtube.com/watch?v=HkgDRRWrZKg

简短: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)
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案给出了不太正确的cookie,例如对于youtube来说它很糟糕...... (5认同)
  • 谷歌修复了这个问题,不再是一个选项。 (4认同)