驱动程序列表在selenium webdriver中被视为字符串

时间:2015-02-12 10:37:18

标签: python selenium selenium-webdriver webdriver

我将所有驱动程序实例的list(driverList[])传递给另一个执行driver.close()以关闭所有打开的浏览器实例的函数。

该列表被视为字符串,即使它是所有驱动程序对象的列表。所以我收到了这个错误

  

' str没有属性关闭'

以下是简单的代码。

def function1(self):
     driver1 = webdriver.Chrome()
     driver1.get(www.google.com)
     driver2 = webdriver.Chrome()
     driver2.get(www.google.com) 
     driverList = [driver1,driver2]
     return driverList

def function2(self,driverList):
     driverList[0].close()
     driverList[1].close()

当我执行第二个函数时,它给了我这个错误

  

' str没有属性关闭'

。请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您在对象的范围内遇到问题。

def function2(self,driverList):
    print driverList      
    driverList[0].close()
    driverList[1].close()

def function1(self, response):
    self.driver1 = webdriver.Firefox()
    self.driver2 = webdriver.Firefox()

    self.driver1.get("http://www.google.com")
    self.driver2.get("http://www.yahoo.com")
    driverList = [self.driver1,self.driver2]
    self.function2(driverList)

打印driverList的输出是:

[<selenium.webdriver.firefox.webdriver.WebDriver object at 0x111d4fc90>, 

<selenium.webdriver.firefox.webdriver.WebDriver object at 0x111d5bd50>]

UPDATE:用于测试和解决问题的完整代码(scrapy + selenium.webdriver)

class WindowSpider1(CrawlSpider):
    name = "windows"
    start_urls = ["http://www.google.com"]


    def function2(self,driverList):
        print driverList
        driverList[0].close()
        driverList[1].close()


    def parse(self, response):
        self.driver1 = webdriver.Firefox()
        self.driver2 = webdriver.Firefox()

        self.driver1.get("http://www.google.com")
        self.driver2.get("http://www.yahoo.com")
        driverList = [self.driver1,self.driver2]
        self.function2(driverList)