首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Python登录Gmail并检测登录失败

使用Python登录Gmail并检测登录失败
EN

Stack Overflow用户
提问于 2021-02-06 11:23:12
回答 3查看 1.4K关注 0票数 1

我知道很多人都问过类似的问题,但是我想知道如何使用python登录到gmail (或google帐户)。我已经有了一段代码(见下面),它可以使用selenium将用户登录到gmail。然而,我注意到了一些问题。

当程序stops/closes.

  • It无法检测到失败的登录时浏览器关闭。

这两个问题都需要解决,我才能在我的项目上工作。我不介意使用像pyautogui这样的selenium以外的东西来打开google。但是,它需要能够检测到失败的登录,然后关闭浏览器,如果登录成功,浏览器应该保持打开状态,供用户使用。

我的代码:

代码语言:javascript
运行
复制
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,但是这并不是很好,现在我已经没有想法了。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-02-14 02:26:47

更新02-14-2021

我能够提取这条错误消息:

使用此代码:

代码语言:javascript
运行
复制
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是:

代码语言:javascript
运行
复制
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()语句。

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
chrome_options.add_experimental_option("detach", True)

我还抛出了这些错误消息:

代码语言:javascript
运行
复制
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')]")

PSEUDOCODE代码:

这是您可以这样做的,但您可能需要在测试时调整代码。

代码语言:javascript
运行
复制
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。我尝试过使用selenium,并得到了这个警告

当我点击学习更多的时,我得到了这条消息。

请注意这一行:是通过软件自动化而不是人工控制的。

以下是他们讨论谷歌直接登录问题的其他问题:

票数 2
EN

Stack Overflow用户

发布于 2021-02-06 11:52:12

您应该查看Gmail的编程访问,它将比像selenium一样尝试驱动UI工作得更好。https://developers.google.com/gmail/api/quickstart/python

票数 1
EN

Stack Overflow用户

发布于 2021-02-13 13:52:47

我现在没有时间去编码,但是:

要使浏览器保持打开状态,只需删除driver.close()函数,这就是程序到达它时停止的原因。

要解决您的问题,只需尝试成功登录和失败登录,都要寻找一个WebElement,它以独特的方式指示其中一个事件,然后,只有当selenium在登录(失败)之后在视图中找到WebElement时,才将driver.close()放在if声明中,否则不要让它执行指令。

就这么简单。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66076295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档