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

driver list is considered as string in selenium webriver

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

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

'str has no attribute close'

下面是简单的代码。

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 has no attribute close'

。请帮忙。提前致谢。

函数之间的对象范围有问题。

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>]

更新: 用于测试和解决问题的完整代码 (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)