首页 > 解决方案 > 我正在使用 python 中的 selenium 并尝试在 chrome 中自动登录谷歌,但我遇到了问题

问题描述

它给出了无法登录的错误。

请帮我

标签: pythonselenium

解决方案


使用此链接:

https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow

我不知道是什么问题,但谷歌有自己的安全协议,很难绕过它。

要使用它,您必须关闭两步验证和其他帐户安全性。就我而言,当我尝试登录时,前 5-6 次它要求我通过点击智能手机中的一个数字来验证(希望你知道那是什么),然后它对我来说工作正常。

现在,如果你想避免错误,你可以在 selenium中使用Explicit Wait 。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec

WebDriverWait(driver, 100).until(ec.visibility_of_element_located((By.XPATH, '###')))
#Place the xpath of  you element in place of ###

通过使用它,您可以让 selenium 等到元素加载然后继续。

如果问题继续出现,您可以使用第三方网站登录,其中包括堆栈溢出。

from selenium import webdriver 
from time import sleep

username=raw_input("username: ") 
password=raw_input("password: ") 
driver=webdriver.Chrome('...')  #path of your chrome driver         
driver.get('https://stackoverflow.com/users/signup') 
sleep(3) 
driver.find_element_by_xpath('//*[@id="openid-buttons"]/button[1]').click() 
driver.find_element_by_id('identifierId').send_keys(username) 
driver.find_element_by_id('identifierNext').click() 
sleep(3) 
driver.find_element_by_name('password').send_keys(password) 
driver.find_element_by_id('passwordNext').click() 
sleep(2) 
driver.get('https://mail.google.com/mail/u/0/#inbox')

现在,您可以获取任何 URL 并继续。


推荐阅读