Dec-11-2022, 04:09 AM
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