我知道很多人都问过类似的问题,但我想知道如何使用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,但是这并不是很好,现在我没有主意了。
我现在没有时间编码,但是:
要使浏览器保持打开状态,只需删除驱动程序。close()函数,这就是当程序到达它时使其停止的原因。
要解决您的问题,只需尝试成功登录和失败登录,在这两种情况下查找以独特方式指示其中一个事件的WebElement,然后放置驱动程序。仅当selenium在登录后(失败的登录)在视口中找到WebElement时,if声明中的close(),否则不要让它执行指令。
就这么简单。
您应该看看用于编程访问的Gmail API,它比像selenium那样驱动UI要好得多。https://developers.google.com/gmail/api/quickstart/python
我能够提取此错误消息:
使用此代码:
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登录中,您将收到以下两条错误消息之一:
获取这些错误消息的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()
如果要保持浏览器打开,请不要使用驱动程序。close()语句,但添加此experimental\u选项
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()
谷歌禁止使用自动脚本登录Gmail。我试过使用硒,我得到了这个警告。
当我点击了解更多时,我收到了这条消息。
请注意这一行:通过软件自动化而不是人工控制
以下是他们讨论Google直接登录问题的其他问题:
>
尝试使用Selenium登录Gmail帐户时出现“此浏览器或应用程序可能不安全”错误
GMail正在阻止通过自动化(Selenium)登录
我正在寻求登录gmail帐户并使用python脚本下载一些电子邮件的帮助。我正在尝试采用这里找到的方法,但我在步骤1中遇到了一个问题,即通过imap访问帐户。 下面是我要开始的代码: 我得到以下错误: 在gmail设置中确实启用了Imap。我已经查看了google支持链接上的说明,以及类似情况下有关此错误的问题,例如这里和这里,但我的情况与第一次不同,因为1)它从一开始就不起作用,2)我没有经常运
问题内容: 我正在寻求一些帮助,以登录gmail帐户并使用python脚本下载一些电子邮件。我正在尝试采用此处找到的方法,但是我遇到了第1步的问题,即通过imap访问帐户。 这是我开始的代码: 我收到以下错误: 在gmail设置中确实启用了Imap。我已经查看了Google支持链接上的说明以及在类似情况下有关此错误的问题,但是我的情况与第一种情况有所不同,因为 1)从一开始它就没有用, 2)我是不
问题内容: 我正在尝试使用Selenium登录gmail。在新的gmail登录中,首先输入您的电子邮件ID,然后在下一页输入密码。电子邮件页面的URL和密码页面的URL不同。因此,当我在其中传递密码URL时,它将重新加载页面,并且如果您刷新URL而未输入密码,它将重定向到电子邮件页面。因此,它缺少密码字段选择器。仍然是先前的网址,即电子邮件页面的网址。这是我的代码。我正在使用Chrome驱动程序和
我有一个硒服务,必须登录到我的gmail帐户作为第一步。这个功能几周前还在工作,但突然登录开始失败,我在浏览器中看到这个错误,在硒Chrome和Firefox驱动程序中都尝试过- 此错误发生在selenium服务插入电子邮件、密码并单击登录按钮之后。谷歌支持论坛也报告了类似的错误:https://support.google.com/accounts/thread/10916318?hl=en他们
问题内容: 我无法摆脱这个错误。我已经通过SSMS添加了“ NT AUTHORITY \ NETWORK”用户以及使用此线程作为参考的相关角色:用户“ NT AUTHORITY \ NETWORKSERVICE”的登录失败 我试图通过Windows服务建立数据库连接。在调试模式下,数据库连接可以正常工作。当我实际尝试运行已安装的服务时,便会出现此错误。 这是我来自app.config的连接字符串:
我目前正在开发一个需要gmail收件箱的应用程序。目前我正在使用带有凭据的Gmail API。json和令牌。pickle,虽然每次使用脚本时我都想使用其他凭据登录,但这很正常。我对这个API很陌生,想知道是否可以使用令牌所用凭据以外的其他凭据登录。泡菜。 Gmail API 要当前获取gmail收件箱,我使用以下代码: imaplib 我尝试获取某个用户收件箱的另一种方法是使用imaplib和电