我正在使用selenium webdriver创建一个用python编写的测试套件。但是,当我运行测试时,我得到一个错误:“PythonOrgSearch”对象没有属性“driver”
我很确定这是因为测试没有按顺序运行,所以驱动程序在测试完成之前关闭。我以前也遇到过错误:“试图在没有建立连接的情况下运行命令”,我想这也表明测试没有按顺序运行,所以驱动程序没有启动?我不确定这是否准确,只是我最好的猜测。我的代码看起来像:import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains
from urllib.request import urlopen
from html.parser import HTMLParser
gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko+'.exe')
class PythonOrgSearch(unittest.TestCase):
#sets up driver to run tests
def setUp(self):
self.driver = driver
self.driver.start()
def test_opens(self):
driver.get("url.com")
driver.find_element_by_id('username').send_keys('user')
driver.find_element_by_id('password').send_keys('pass')
driver.find_elements_by_css_selector("button[type='submit']")[0].click()
time.sleep(2);
self.assertIn("title", driver.title)
def ztearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
编辑:我添加了驱动程序=自驱动程序在每个功能开始时