gas*_*shu 10 selenium google-chrome webdriver selenium-chromedriver selenium-webdriver
我有一个 Selenium 服务,第一步必须登录到我的 gmail 帐户。此功能在几周前开始工作,但突然登录开始失败,我在浏览器中看到此错误,在 selenium 中的 Chrome 和 Firefox 驱动程序中都尝试过 -
此错误是在 selenium 服务插入电子邮件、密码并单击登录按钮后出现的。谷歌支持论坛也报告了类似的错误 - https://support.google.com/accounts/thread/10916318?hl=en,他们说“谷歌似乎在他们的登录流程中引入了自动化工具检测!” 但在这个线程中没有解决方案。
其他一些可能有用的细节-
如果您需要查看代码,请告诉我,我会在此处发布。提前致谢!
编辑 添加示例代码以供参考。
public void loginGoogleAccount(String emailId, String password) throws Exception {
ChromeOptions options = new ChromeOptions();
options.addArguments("--profile-directory=Default");
options.addArguments("--whitelisted-ips");
options.addArguments("--start-maximized");
options.addArguments("--disable-extensions");
options.addArguments("--disable-plugins-discovery");
WebDriver webDriver = new ChromeDriver(options);
webDriver.navigate().to("https://accounts.google.com");
Thread.sleep(3000);
try {
WebElement email = webDriver.findElement(By.xpath("//input[@type='email']"));
email.sendKeys(emailId);
Thread.sleep(1000);
WebElement emailNext = webDriver.findElement(By.id("identifierNext"));
emailNext.click();
Thread.sleep(1000);
WebDriverWait wait = new WebDriverWait(webDriver, 60);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("identifierNext")));
Thread.sleep(3000);
WebElement passwordElement = webDriver.findElement(By.xpath("//input[@type='password']"));
passwordElement.sendKeys(password);
Thread.sleep(1000);
WebElement passwordNext = webDriver.findElement(By.id("passwordNext"));
passwordNext.click();
} catch (Exception e) {
LOGGER.info(String.format("No email/password field available or it is already logged in: [%s]: ",
e.getMessage()));
}
}
Run Code Online (Sandbox Code Playgroud)
切换“允许不太安全的应用程序访问”
您的帐户中有一个设置,您可以切换它可能有助于解决此问题。它是“允许不太安全的应用程序访问”。如果您已经登录要修改的 Gmail 帐户,您应该可以访问下面的链接来切换该设置。
更改 google 帐户设置的链接:https : //myaccount.google.com/lesssecureapps
更多信息(来源):https : //support.google.com/accounts/answer/6010255
小智 1
此问题是由于硒铬配置文件造成的。创建一个新的 chrome 配置文件并使用您遇到问题的电子邮件 ID 登录该配置文件。然后打开同步。
有了这个 chrome 配置文件,我可以跳过登录步骤并直接执行主要流程。使用:Chrome Options添加新创建的 chrome 配置文件作为参数。
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("Binary path of the Chrome");
Run Code Online (Sandbox Code Playgroud)
希望这个对你有帮助。
| 归档时间: |
|
| 查看次数: |
10602 次 |
| 最近记录: |