Python Forum
Getting NameError for a function that is defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting NameError for a function that is defined
#1
So far, it's a simple class, but I can't understand why I get a NameError for a function that exists in the class. The function is declared on line 15, 'getby', then called on line 25. My code editor (VS Code) underlines it wherever it is called, hinting that it isn't defined, as if it doesn't exist. I have changed the name with the same defect. Any reason this is happening? Thanks

class Browser:
    def __init__(self) -> None:
        try:
            chrome_options = webdriver.ChromeOptions()
            chrome_options.add_argument("--start-maximized")
            chrome_options.add_argument('--disable-infobars')
            pth=r'C:\Users\Jonwa\AppData\Local\Google\Chrome\User Data\Profile 2'
            chrome_options.add_argument(f'--user-data-dir={pth}')
            chrome_options.add_argument('--disable-blink-features=AutomationControlled')
            chrome_options.add_argument("--disable-popup-blocking")
            self.driver = uc.Chrome(options=chrome_options)
        except Exception as e:
            print('Fail to create browser')

    def getby(self, locator_type):
        by_locators=['class', 'xpath', 'id', 'tag', 'name']
        by_map={'class':By.CLASS_NAME, 'xpath':By.XPATH, 'id':By.ID, 'tag':By.TAG_NAME, 'name':By.NAME}
        fb=locator_type.lower()
        try:
            return by_map[fb]
        except Exception as e:
            print(e)

    def click_element(self,find_by,locator,wait_by=None,wait_locator=None,wait_time=10):
        by_type=getby(find_by)
        try:
            el=self.driver.find_element(by_type,locator)
            el.click()
            if wait_locator:
                by_type=getby(wait_by)
                wait=WebDriverWait(self.driver,wait_time)
                while True:
                    try:
                        wait.until(EC.visibility_of_element_located((by_type,wait_locator)))
                        break
                    except TimeoutError as t:
                        print('Waiting for page authentication')
                    except Exception as e:
                        raise e
            sleep(2)
        except Exception as e:
            print(e)
Error:
NameError Traceback (most recent call last) Cell In [8], line 5 3 br=Browser() 4 br.browse_site(home_url,"//div[@id='Home']") ----> 5 br.click_element('class','anchorButton','class','rows-per-page') 6 br.click_element('xpath',"//ul/li[5]/a") 7 br.text_to_element('xpath',"//input[@class='hasDatepicker']",start_date) Cell In [7], line 53, in Browser.click_element(self, find_by, locator, wait_by, wait_locator, wait_time) 52 def click_element(self,find_by,locator,wait_by=None,wait_locator=None,wait_time=10): ---> 53 by_type=getby(find_by) 54 try: 55 el=self.driver.find_element(by_type,locator) NameError: name 'getby' is not defined
Reply
#2
There is no function named getby. class Browser has a method named getby. You can call this method using Browser.getby(browser_instance, locator_type), or use the more common syntax browser_instance.getby(locator_type). In your particular case you have a handy browser instance named "self" (use self.getby(find_by)).
Reply
#3
(Dec-11-2022, 06:25 AM)deanhystad Wrote: There is no function named getby. class Browser has a method named getby. You can call this method using Browser.getby(browser_instance, locator_type), or use the more common syntax browser_instance.getby(locator_type). In your particular case you have a handy browser instance named "self" (use self.getby(find_by)).

I knew it would be a simple oversight like that. Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: name 'pi' is not defined katebishop 2 1,343 Jul-15-2024, 05:48 AM
Last Post: Pedroski55
  I'm getting a NameError: ...not defined. vonArre 2 1,956 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 1,154 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 3,622 Sep-03-2023, 03:22 PM
Last Post: deanhystad
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 4,140 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,868 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 4,559 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 2,021 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  User-defined function to reset variables? Mark17 3 2,395 May-25-2022, 07:22 PM
Last Post: Gribouillis
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 2,562 May-13-2022, 03:37 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020