在以下输入字段上使用Scrapy和send_keys

时间:2015-08-22 20:52:47

标签: python selenium input selenium-webdriver scrapy

我正在尝试关注twitter.com/login和send_keys('foo')上的电子邮件输入,但我没有运气。有人能告诉我为什么我的实施是错误的:

import time

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector

from selenium import webdriver 

class MySpider(BaseSpider):
    name = "new"
    allowed_domains = ["twitter.com"]
    start_urls = ["https://www.twitter.com/login"]

    def __init__(self):
    self.driver = webdriver.Firefox()

    def parse(self, response):
    self.driver.get(response.url)
    time.sleep(1)

    #print response.body
    username = self.driver.find_element_by_class_name('email-input')
    username.click()
    username.clear()
    username.send_keys('foo')


    #self.driver.close()

任何帮助将不胜感激。另外,当我尝试这个时,我收到以下错误:

  

2015-08-22 15:44:09-0500 [scrapy] DEBUG:telnet控制台监听127.0.0.1:6023   2015-08-22 15:44:10-0500 [新] DEBUG:重定向(301)到https://twitter.com/login>来自https://www.twitter.com/login>   2015-08-22 15:44:10-0500 [新] DEBUG:Crawled(200)https://twitter.com/login> (引用者:无)   2015-08-22 15:44:22-0500 [new]错误:蜘蛛错误处理https://twitter.com/login> (引用者:无)       Traceback(最近一次调用最后一次):         在mainLoop中输入文件“/usr/lib/python2.7/dist-packages/twisted/internet/base.py”,第1201行           self.runUntilCurrent()         在runUntilCurrent中的文件“/usr/lib/python2.7/dist-packages/twisted/internet/base.py”,第824行           call.func(* call.args,** call.kw)         回调文件“/usr/lib/python2.7/dist-packages/twisted/internet/defer.py”,第382行           self._startRunCallbacks(结果)         _startRunCallbacks中的文件“/usr/lib/python2.7/dist-packages/twisted/internet/defer.py”,第490行           self._runCallbacks()       --- ---         _runCallbacks中的文件“/usr/lib/python2.7/dist-packages/twisted/internet/defer.py”,第577行           current.result = callback(current.result,* args,** kw)         文件“/home/tyrick/Documents/twitter_followers/twitter_followers/spiders/newUsers.py”,第22行,解析           username.click()         单击文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py”,第65行           self._execute(Command.CLICK_ELEMENT)         _execute中的文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py”,第385行           return self._parent.execute(command,params)         文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”,第173行,执行           self.error_handler.check_response(响应)         在check_response中输入文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py”,第166行           提出exception_class(消息,屏幕,堆栈跟踪)       selenium.common.exceptions.ElementNotVisibleException:消息:元素当前不可见,因此可能无法与之交互       堆栈跟踪:           at fxdriver.preconditions.visible(file:///tmp/tmpHPD9xT/extensions/fxdriver@googlecode.com/components/command-processor.js:8959:12)           在DelayedCommand.prototype.checkPreconditions_(文件:///tmp/tmpHPD9xT/extensions/fxdriver@googlecode.com/components/command-processor.js:11618:15)           在DelayedCommand.prototype.executeInternal_ / h(文件:///tmp/tmpHPD9xT/extensions/fxdriver@googlecode.com/components/command-processor.js:11635:11)           在DelayedCommand.prototype.executeInternal_(文件:///tmp/tmpHPD9xT/extensions/fxdriver@googlecode.com/components/command-processor.js:11640:7)           在DelayedCommand.prototype.execute /< (文件:///tmp/tmpHPD9xT/extensions/fxdriver@googlecode.com/components/command-processor.js:11582:5)

1 个答案:

答案 0 :(得分:2)

问题是,有多个元素email-input,第一个实际定位的元素是不可见的。您需要页面容器中的那个:

self.driver.find_element_by_css_selector("#page-container .email-input")