使用Python-Selenium自动执行GMAIL登录 [英] Automating GMAIL login using Python-Selenium

查看:203
本文介绍了使用Python-Selenium自动执行GMAIL登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python的Selenium软件包自动登录到GMail.但是,我无法完成任务并出现以下错误:

I am trying to automate logging into GMail using Selenium package of Python. However, I am not able to accomplish the task and get the following error:

Traceback (most recent call last):
  File "C:\Users\Surojit\Desktop\Python\automaticpasswordFiller.py", line   21, in <module>
    passwordElem = browser.find_element_by_id('Passwd')
  File "C:\Users\Surojit\AppData\Local\Programs\Python\Python35-32\lib\site- packages\selenium\webdriver\remote\webdriver.py", line 266, in  find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\Surojit\AppData\Local\Programs\Python\Python35-32\lib\site- packages\selenium\webdriver\remote\webdriver.py", line 744, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Users\Surojit\AppData\Local\Programs\Python\Python35-32\lib\site-  packages\selenium\webdriver\remote\webdriver.py", line 233, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Surojit\AppData\Local\Programs\Python\Python35-32\lib\site- packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate  element: {"method":"id","selector":"Passwd"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_   (file:///C:/Users/Surojit/AppData/Local/Temp/tmpceecsm46/extensions/fxdriver@goo glecode.com/components/driver-component.js:10770)
     at FirefoxDriver.prototype.findElement   (file:///C:/Users/Surojit/AppData/Local/Temp/tmpceecsm46/extensions/fxdriver@goo  glecode.com/components/driver-component.js:10779)
    at DelayedCommand.prototype.executeInternal_/h   (file:///C:/Users/Surojit/AppData/Local/Temp/tmpceecsm46/extensions/fxdriver@goo    glecode.com/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_    (file:///C:/Users/Surojit/AppData/Local/Temp/tmpceecsm46/extensions/fxdriver@goo glecode.com/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/<   (file:///C:/Users/Surojit/AppData/Local/Temp/tmpceecsm46/extensions/fxdriver@googlecode.com/components/command-processor.js:12608) 

我编写的简单代码是:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

browser = webdriver.Firefox()
browser.get('http://gmail.com')
action = webdriver.ActionChains(browser)
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys("MyUserName")
browser.find_element_by_name('signIn').click()
#browser.get('https://accounts.google.com/ServiceLogin?         service=mail&continue=https://mail.google.com/mail/#password')
passwordElem = browser.find_element_by_id('Passwd')
passwordElem.send_keys("MyPassword")
browser.find_element_by_name('signIn').click()

此外,我试图通过与以下类似问题的答案进行比较来找出代码中的错误:

Also, I have tried to find out the error in my code by comparing it to an answer given to a similar question here at: Auto connect on my Gmail account with Python Selenium

有人可以引导我走上正确的道路,让我知道我在哪里犯错吗?

Can someone please guide me on the right path and let me know where I am making a mistake?

P.S:这是我关于stackoverflow的第一篇文章.请原谅我在发布问题时犯的任何错误

P.S: This is my first post on stackoverflow. Please excuse me for any mistake that I have made in posting the question

推荐答案

您正在尝试查找尚未加载dom的元素的Passwd id.尝试添加一些延迟,以便页面可以加载.

You are trying to find the Passwd id of the element which is not loaded in dom yet. Try adding some delay so that the page could load.

emailElem = browser.find_element_by_id('Email')
emailElem.send_keys('MyUserName')
nextButton = browser.find_element_by_id('next')
nextButton.click()
time.sleep(1)
passwordElem = browser.find_element_by_id('Passwd')
passwordElem.send_keys('MyPassword')
signinButton = browser.find_element_by_id('signIn')
signinButton.click()

推荐的方法是browser.implicitly_wait(num_of_seconds),请参见

recommended method is browser.implicitly_wait(num_of_seconds) see this

这篇关于使用Python-Selenium自动执行GMAIL登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆