1

I use this post http://www.autotest.org.ua/first-autotest-with-selenium-webdriver-and-python/ and made project in PyCharm

This is a photo

Code trials:

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)

Errors:

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

I dont understand how to fix it...

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

-1

This error message...

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

...implies that the unittest have an initialization error.

I don't see any such error in your code block as such but there is an issue in the setUp() method. A few words:

  • def setUp(self):: The setUp() is the part of initialization and this method will get called before every test function which you are going to write in this testcase class. You have misspelt setUp(self) as setUpp(self).
  • If you are using webdriver.Chrome() you need to pass the absolute path of the chromedriver but you have provided geckodriver.
  • While you pass the Key executable_path provide the Value through single quotes along with the raw r switch.
  • def tearDown(self):: The tearDown() method is called after every test method. This is the method to do all the cleanup actions.
  • if __name__ == '__main__':: This line sets the __name__ variable to have a value "__main__". If this file is being imported from another module then __name__ will be set to the other module's name.
  • You will find a detailed discussion in What does if name == “main”: do?
  • With the above mentioned points, your effective code block will be:

    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()
    
  • You can find a relevant discussion in Python + WebDriver — No browser launched while using unittest module

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352