2015-11-14 38 views
0

我試圖自動登錄與我的Gmail在Python解釋硒,但我得到這個錯誤:自動在我的Gmail帳戶與Python硒連接

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with 

我的代碼如下所示:

#!/usr/bin/python 
# coding: utf8 

import scrapy 

from selenium import webdriver 
from scrapy.selector import Selector 
from selenium.webdriver.common.action_chains import ActionChains 
from scrapy.contrib.spiders import CrawlSpider 
from scrapy.selector import HtmlXPathSelector 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.http import Request, FormRequest 
from scrapy import log 
from scrapy.exceptions import DropItem 
from scrapy import signals 
from selenium.webdriver.common.by import By 
import scrapy 
from scrapy import signals 
from scrapy.http import TextResponse 
from scrapy.xlib.pydispatch import dispatcher 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 


class googleplay(CrawlSpider): 
    name = "selenium" 
    flag = True 

    def __init__(self): 
     self.driver = webdriver.Firefox() 
     self.driver.get('http://gmail.com') 
     action = webdriver.ActionChains(self.driver) 
     #User name input field identification and data entered 
     usernametext = self.driver.find_element_by_name('Email') 
     usernametext.send_keys("[email protected]") #put your actual username 
     self.driver.find_element_by_name('signIn').click() 
     time.sleep(2) 
     #Password input field identification and data entered 
     passwordtext = self.driver.find_element_by_name('Passwd') 
     passwordtext.send_keys("password") #put your actual password 
     key = self.driver.find_element_by_name('signIn') 
     print "----------------------" 
     key.click() 
     print "----------------------" 
     start_urls = ["https://www.mywebsite.com"]  

    def parse(self, reponse): 
     #Loading the gmail URL 
     print "toto" 

我該如何解決我的問題?

+0

你確定你不想使用這個[API](https://github.com/charlierguo/gmail)? –

+0

您是否嘗試過更改網址?使用'https://accounts.google.com/ServiceLogin?service = mail#identifier'而不是'http:// gmail.com' – Rahul

+0

我需要使用硒以廢棄一些信息,之後將進行身份驗證。 .. –

回答

2

嘗試更改網址。使用https://accounts.google.com/ServiceLogin?service=mail#identifier而不是http://gmail.com

請確保您使用您的原始電子郵件和密碼。並且將每個元素的self.driver.find_element_by_name更改爲self.driver.find_element_by_id

您的代碼將是:

#!/usr/bin/python 
# coding: utf8 

import scrapy 
import time 
from selenium import webdriver 
from scrapy.selector import Selector 
from selenium.webdriver.common.action_chains import ActionChains 
from scrapy.contrib.spiders import CrawlSpider 
from scrapy.selector import HtmlXPathSelector 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.http import Request, FormRequest 
from scrapy import log 
from scrapy.exceptions import DropItem 
from scrapy import signals 
from selenium.webdriver.common.by import By 
from scrapy import signals 
from scrapy.http import TextResponse 
from scrapy.xlib.pydispatch import dispatcher 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 


class googleplay(CrawlSpider): 
    name = "selenium" 
    flag = True 

    def __init__(self): 
     self.driver = webdriver.Firefox() 
     self.driver.get('http://gmail.com') 
     action = webdriver.ActionChains(self.driver) 
     #User name input field identification and data entered 
     usernametext = self.driver.find_element_by_name('Email') 
     usernametext.send_keys("[email protected]") #put your actual username 
     self.driver.find_element_by_name('signIn').click() 
     #Password input field identification and data entered 
     passwordtext = self.driver.find_element_by_id('Passwd') 
     passwordtext.send_keys("mypassword") #put your actual password 
     self.driver.find_element_by_id('signIn').click() 
     print "----------------------" 
     #key.click() 
     print "----------------------" 
     start_urls = ["https://www.mywebsite.com"]  

    def parse(self, reponse): 
     #Loading the gmail URL 
     print "toto" 
+0

我有同樣的錯誤... –

+0

這對我來說工作得很好。您還需要導入時間模塊:'導入時間'。您也可以嘗試更改此行'usernametext = self.driver.find_element_by_id('電子郵件')' – Rahul

+0

並確保使用您的原始電子郵件和密碼。並將每個元素的'self.driver.find_element_by_name'更改爲'self.driver.find_element_by_id'。 – Rahul