我知道很多人都问过类似的问题,但是我想知道如何使用python登录到gmail (或google帐户)。我已经有了一段代码(见下面),它可以使用selenium将用户登录到gmail。然而,我注意到了一些问题。
当程序stops/closes.
这两个问题都需要解决,我才能在我的项目上工作。我不介意使用像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-02-14 02:26:47
更新02-14-2021
我能够提取这条错误消息:
使用此代码:
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()
如果希望使浏览器保持打开状态,则不要使用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')]")
PSEUDOCODE代码:
这是您可以这样做的,但您可能需要在测试时调整代码。
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,并得到了这个警告。
当我点击学习更多的时,我得到了这条消息。
请注意这一行:是通过软件自动化而不是人工控制的。
以下是他们讨论谷歌直接登录问题的其他问题:
发布于 2021-02-06 11:52:12
您应该查看Gmail的编程访问,它将比像selenium一样尝试驱动UI工作得更好。https://developers.google.com/gmail/api/quickstart/python
发布于 2021-02-13 13:52:47
我现在没有时间去编码,但是:
要使浏览器保持打开状态,只需删除driver.close()函数,这就是程序到达它时停止的原因。
要解决您的问题,只需尝试成功登录和失败登录,都要寻找一个WebElement,它以独特的方式指示其中一个事件,然后,只有当selenium在登录(失败)之后在视图中找到WebElement时,才将driver.close()放在if声明中,否则不要让它执行指令。
就这么简单。
https://stackoverflow.com/questions/66076295
复制相似问题