社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

AttributeError:“GoogleSearch”对象在通过Python unittest执行测试时没有属性“driver”

Александр ВолкоР• 5 年前 • 1908 次点击  

我用这个帖子 http://www.autotest.org.ua/first-autotest-with-selenium-webdriver-and-python/ 在皮查姆做项目

This is a photo

代码测试:

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

我不知道怎么解决。。。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50047
 
1908 次点击  
文章 [ 1 ]  |  最新文章 5 年前
DebanjanB
Reply   •   1 楼
DebanjanB    6 年前

此错误消息。。。

AttributeError: 'GoogleSearch' object has no attribute 'driver'

…意味着 单元测试 出现初始化错误。

我在你的代码块中没有看到这样的错误,但是 setUp() 方法。几句话:

  • def setUp(self): :的 设置() 是初始化的一部分,这个方法将在您将要在这个testcase类中编写的每个测试函数之前被调用。你拼错了 setUp(self) 作为 setUpp(self) .
  • 如果你正在使用 webdriver.Chrome() 你需要通过 绝对路径 铬河 但你提供了 壁虎 .
  • 当你经过 钥匙 executable_path 提供 价值 通过单引号和原始的 r 切换。
  • def tearDown(self): :的 tearDown() 方法在每个测试方法之后调用。这是执行所有清理操作的方法。
  • if __name__ == '__main__': :此行设置 __name__ 具有值的变量 "__main__" . 如果此文件是从另一个模块导入的,则 __姓名__ 将被设置为另一个模块的名称。
  • 您可以在中找到详细的讨论 What does if name == “ main ”: do?
  • 有了上述几点,您的有效代码块将是:

    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()
    
  • 你可以在 Python + WebDriver — No browser launched while using unittest module