Home > OS >  NameError: name 'driver' is not defined although imported webdriver library
NameError: name 'driver' is not defined although imported webdriver library

Time:10-07

def test_search(self):
        xpath_search = "/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"
        btn_search = '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[2]/div[2]/div[5]/center/input[1]'
        wait = WebDriverWait(driver, 10)
        
        #click search and input 'youtube'
        self.wait.until(EC.element_to_be_clickable((By.XPATH, xpath_search))).send_keys('youtube')
        
        #click button search
        self.wait.until(EC.element_to_be_clickable((By.XPATH, btn_search))).click()

Output: E

ERROR: test_search (main.Search)

Traceback (most recent call last): File "C:\Users\ththiep\Desktop\runner\Newcommer\searchGG_ASON.py", line 21, in test_search wait = WebDriverWait(driver, 10) NameError: name 'driver' is not defined


Ran 1 test in 8.475s

FAILED (errors=1) [Finished in 11.4s]

CodePudding user response:

Seems like you have not defined the variable 'driver'. You need to do it somewhat similar to as shown below

from selenium import webdriver

driver = webdriver.Chrome() 

Customize it as needed

CodePudding user response:

class stuff():
    def __init__(self):
            self.driver=webdriver.Chrome()
    def test_search(self):
            xpath_search = "/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"
            btn_search = '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[2]/div[2]/div[5]/center/input[1]'
            self.wait = WebDriverWait(self.driver, 10)
            
            #click search and input 'youtube'
            self.wait.until(EC.element_to_be_clickable((By.XPATH, xpath_search))).send_keys('youtube')
            
            #click button search
            self.wait.until(EC.element_to_be_clickable((By.XPATH, btn_search))).click()

You should allocate a driver variable to that class using self. then access it using self.driver.

  • Related