python - 使用 Python 登录 Gmail 并检测登录失败
问题描述
我知道很多人对此提出了类似的问题,但是我想知道如何使用 python 登录到 gmail(或 google 帐户)。我已经有一个代码(见下文),可以使用 selenium 将用户登录到 gmail。但是我注意到了一些问题。
- 当程序停止/关闭时浏览器关闭。
- 它无法检测到失败的登录。
这两个问题确实需要解决,我才能在我的项目上工作。我不介意使用 pyautogui 之类的 selenium 以外的东西来打开 google。但是,它需要能够检测到登录失败然后关闭浏览器,如果登录成功,浏览器应该保持打开状态供用户使用。
我的代码:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
def gmail_login(username, password):
gmailId = username
passWord = password
driver = webdriver.Chrome(ChromeDriverManager().install())
try:
driver.get(r'https://accounts.google.com/signin/v2/identifier?continue=' + \
'https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1' + \
'&flowName=GlifWebSignIn&flowEntry = ServiceLogin')
driver.implicitly_wait(3)
loginBox = driver.find_element_by_xpath('//*[@id ="identifierId"]')
loginBox.send_keys(gmailId)
nextButton = driver.find_elements_by_xpath('//*[@id ="identifierNext"]')
nextButton[0].click()
passWordBox = driver.find_element_by_xpath(
'//*[@id ="password"]/div[1]/div / div[1]/input')
passWordBox.send_keys(passWord)
nextButton = driver.find_elements_by_xpath('//*[@id ="passwordNext"]')
nextButton[0].click()
except:
driver.close()
gmail_login("email@gmail.com", "Password")
如果它等于登录的 url,我会在程序完成后检查 url,但是它并没有真正工作得很好,现在我没有想法了。
解决方案
2021 年 2 月 14 日更新
我能够提取此错误消息:
使用此代码:
signin_failure = driver.find_elements_by_xpath("//*[contains(text(),'Couldn’t sign you in')]")
if signin_failure[1].text == 'Couldn’t sign you in':
print('something went wrong')
在正常的 gmail 登录中,您将收到以下两条错误消息之一:
- 找不到您的 Google 帐户
- 密码错误。重试或单击忘记密码以重置它
获取这些错误消息的 XPATH 是:
wrong_email = driver.find_elements_by_xpath("//*[contains(text(),'Couldn’t find your Google Account')]")
wrong_password = driver.find_elements_by_xpath("//*[contains(text(),'Wrong password. Try again or click Forgot password to reset it')]")
如果您想在出现错误消息(例如无法登录)后关闭浏览器,请添加driver.close()语句。
signin_failure = driver.find_elements_by_xpath("//*[contains(text(),'Couldn’t sign you in')]")
if signin_failure[1].text == 'Couldn’t sign you in':
print('something went wrong')
driver.close()
如果你想保持浏览器打开,那么不要使用driver.close()语句,而是添加这个experimental_option
chrome_options.add_experimental_option("detach", True)
我还能够抛出这些错误消息:
signin_failure = driver.find_elements_by_xpath("//*[contains(text(),'Couldn’t sign you in')]")
# this message was throw when the next button was clicked prior to entering a username
no_input = driver.find_elements_by_xpath("//*[contains(text(),'Enter a valid email of phone number')]")
伪代码:
这就是您可以做到的方式,但您可能必须在测试时调整代码。
def gmail_login(username, password):
driver = webdriver.Chrome(ChromeDriverManager().install())
try:
driver.get(r'https://accounts.google.com/signin/v2/identifier?continue=' + \
'https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1' + \
'&flowName=GlifWebSignIn&flowEntry = ServiceLogin')
driver.implicitly_wait(3)
loginBox = driver.find_element_by_xpath('//*[@id ="identifierId"]')
loginBox.send_keys(username)
nextButton = driver.find_elements_by_xpath('//*[@id ="identifierNext"]')
nextButton[0].click()
wrong_email = driver.find_elements_by_xpath("//*[contains(text(),'Couldn’t find your Google Account')]")
# you need to check this slice
if wrong_email[1].text == 'Couldn’t find your Google Account':
print('something went wrong')
driver.close()
else:
passWordBox = driver.find_element_by_xpath('//*[@id ="password"]/div[1]/div / div[1]/input')
passWordBox.send_keys(password)
nextButton = driver.find_elements_by_xpath('//*[@id ="passwordNext"]')
nextButton[0].click()
wrong_password = driver.find_elements_by_xpath("//*[contains(text(),'Wrong password. Try again or click Forgot password to reset it')]")
# you need to check this slice
if wrong_password[1].text == 'Wrong password. Try again or click Forgot password to reset it':
print('something went wrong')
driver.close()
except:
driver.close()
原帖
Google 禁止使用自动脚本登录 Gmail。我曾尝试使用 selenium 并收到此警告。
当我单击了解更多时,我会收到此消息。
请注意这一行: 正在通过软件自动化而不是人为控制
以下是他们讨论此 Google 直接登录问题的解决方法的其他问题:
推荐阅读
- c# - 我应该在控制台应用程序中使用 System.Drawing 吗?
- datepicker - SwiftUI - 选择选取器时关闭键盘
- swift - 如何修复 onReceive 方法调用的这个 Swift 编译器错误?
- c# - VSTO Office 365:打开 Excel 文档时出错
- android - 在多模块应用程序中使用 koin
- php - 在 wordpress 的主页上添加简码时更新失败
- api - 是否可以限制我来自 Amadeus API 的请求
- c# - 如何从字符串中获取整数数组
- elasticsearch - Elastic 7.X 根映射定义具有不受支持的参数
- sql - 如何循环遍历一个表并根据另一个表中的数据更新特定列?