【问题标题】:AttributeError: 'GoogleSearch' object has no attribute 'driver' while executing tests through Python unittestAttributeError:“GoogleSearch”对象在通过 Python unittest 执行测试时没有属性“驱动程序”
【发布时间】:2019-06-18 13:21:56
【问题描述】:

我使用这篇文章http://www.autotest.org.ua/first-autotest-with-selenium-webdriver-and-python/ 并在 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

我不明白如何解决它...

【问题讨论】:

    标签: python selenium selenium-webdriver webdriver python-unittest


    【解决方案1】:

    此错误消息...

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

    ...暗示 unittest 有初始化错误。

    我在您的代码块中没有看到任何此类错误,但 setUp() 方法中存在问题。几句话:

    • def setUp(self):setUp() 是初始化的一部分,这个方法将在你要在这个测试用例类中编写的每个测试函数之前被调用。您将 setUp(self) 拼写为 setUpp(self)
    • 如果您使用webdriver.Chrome(),您需要传递chromedriver绝对路径,但您已提供geckodriver
    • 在传递 Key 时,executable_path 通过单引号和原始 r 开关提供 Value
    • def tearDown(self):tearDown() 方法在每个测试方法之后调用。这是执行所有清理操作的方法。
    • if __name__ == '__main__'::此行将__name__ 变量设置为具有值"__main__"。如果这个文件是从另一个模块导入的,那么__name__ 将被设置为另一个模块的名称。
    • 您将在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找到相关讨论

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多