属性错误:';谷歌搜索&x27;对象没有属性';驱动程序&x27;在通过Python unittest执行测试时

属性错误:';谷歌搜索&x27;对象没有属性';驱动程序&x27;在通过Python unittest执行测试时,python,selenium,selenium-webdriver,webdriver,python-unittest,Python,Selenium,Selenium Webdriver,Webdriver,Python Unittest,我用这篇文章和PyCharm做了一个项目 代码试用: from selenium import webdriver import unittest from selenium.webdriver.common.keys import Keys class GoogleSearch(unittest.TestCase): def setUpp(self): self.driver = webdriver.Chrome(executable_path="C:\Pytho

我用这篇文章和PyCharm做了一个项目

代码试用:

from selenium import webdriver
import unittest
from selenium.webdriver.common.keys import Keys


class GoogleSearch(unittest.TestCase):
    def setUpp(self):
        self.driver = webdriver.Chrome(executable_path="C:\Python37-32\geckodriver-v0.23.0-win64\geckodriver.exe")
        self.driver.get('https://www.google.by')
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def test_01(self):
        driver = self.driver
        input_field = driver.find_element_by_class_name('class="gLFyf gsfi"')
        input_field.send_keys('python')
        input_field.send_keys(Keys.ENTER)
错误:

FAILED (errors=1)

Error
Traceback (most recent call last):
  File "C:\Python37-32\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Python37-32\lib\unittest\case.py", line 615, in run
    testMethod()
  File "D:\QA\untitled\test.py", line 13, in test_01
    driver = self.driver
AttributeError: 'GoogleSearch' object has no attribute 'driver'


Process finished with exit code 1
我不知道如何修复它…

此错误消息

AttributeError: 'GoogleSearch' object has no attribute 'driver'
…表示单元测试有初始化错误

我在您的代码块中没有看到任何这样的错误,但是
setUp()
方法中有一个问题。几句话:

  • def setUp(self):
    setUp()
    是初始化的一部分,此方法将在您将在此testcase类中编写的每个测试函数之前被调用。您将
    setUp(self)
    错误拼写为
    setUpp(self)
  • 如果您使用的是
    webdriver.Chrome()
    ,则需要通过chromedriver的绝对路径,但您已经提供了geckodriver
  • 传递键
    executable\u path
    时,通过单引号和原始
    r
    开关提供值
  • def tearDown(self):
    :在每个测试方法之后调用
    tearDown()
    方法。这是执行所有清理操作的方法
  • 如果
    名称:此行将
    \uuuuuuuu name\uuuuuuuuu
    变量设置为具有值
    “\uuuuuuu main\uuuuuuuuu”
    。如果此文件是从另一个模块导入的,则
    \uuuu name\uuuu
    将设置为另一个模块的名称
  • 您将在中找到详细的讨论
  • 根据上述几点,您的有效代码块将是:

    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    class GoogleSearch(unittest.TestCase):
    
        def setUp(self):
            self.driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe')
            self.driver.get('https://www.google.by')
            self.driver.maximize_window()
            self.driver.implicitly_wait(10)
    
        def test_01(self):
            driver = self.driver
            input_field = driver.find_element_by_class_name('class="gLFyf gsfi"')
            input_field.send_keys('python')
            input_field.send_keys(Keys.ENTER)
    
        def tearDown(self):
            self.driver.quit()
    
    if __name__ == "__main__":
        unittest.main()
    
  • 您可以在中找到相关的讨论