selenium:来自下一个函数的driver.close()

时间:2014-08-13 05:30:59

标签: python selenium-webdriver scrapy

您好我使用selenium在函数解析中打开一个窗口
我想在函数spider_detail中关闭它 我该怎么做??请指导我。谢谢

class ProductSpider(Spider):
    name = "debug1"
    start_urls = ['http://www.test.tw/in/5/'] 
    def parse(self, response):
        driver = webdriver.Firefox()  #open a window
        driver.get(response.url)
        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.media-body")))
   def spider_detail(self, spider):
        driver.close()        #close the window

2 个答案:

答案 0 :(得分:0)

通常我们使用,driver.quit()

关闭驱动程序
def spider_detail(self, spider):
        driver.quit()  

试试这个。

答案 1 :(得分:0)

我相信你正在使用scrapy,我通常会这样做

from scrapy import signals
from scrapy.xlib.pydispatch import dispatcher

class ProductSpider(Spider):
    name = "debug1"
    start_urls = ['http://www.test.tw/in/5/']

    def __init__(self):
        self.driver = webdriver.Firefox()
        dispatcher.connect(self.close, signals.spider_closed)

    def close(self, spider):
        self.driver.quit()

    def parse(self, response):
        self.driver.get(response.url)
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.media-body")))

    def spider_detail(self, spider):
        self.driver.close()