Vinod
Vinod

Reputation: 1290

local variable 'browser' referenced before assignment in python-selenium

I knew there is lot of same questions-answer available but give me some opportunity to explain I thought it is not with some referencing issue,

Requirement: we have E2E test running already on every 1 hour in Linux VM, but from today it is failing, below is code snippet that set 'browser' variable,

    import webdriver_manager.chrome
    from selenium import webdriver
    
    try:         
       chrome_opt = webdriver.ChromeOptions()
       chrome_opt.add_argument('--headless')
       chrome_opt.add_argument('--no-sandbox')
       # raise Exception(f'inside test error: for re-producing above error')
       browser = webdriver.Chrome(webdriver_manager.chrome.ChromeDriverManager().install(),
                               chrome_opt=chrome_opt)
       # some e2e test-cases

    except TimeoutException as ex:
        browser.quit()
        raise Exception(f'Time-out exception {str(ex)}')
    except Exception as ex:
        browser.quit()
        raise Exception(f'Error while testing user interface: {str(ex)}')

Above code reside is in same file 'test.py' with specific and generic block to catch exception so before line number 7 there is some weird error is generated and that's why exception is raised.

Now the problem is here exception 'local variable 'browser' referenced before assignment' is not caught by above 2 exception blocks(basically generic block) instead it is caught in caller/parent file name as 'main_ui.py' and there generic block.

Here my intention/objective to caught above exception in generic block of same file i.e. 'test.py' and do something like this,

except Exception as ex:
    if browser is not None: 
      browser.quit()
    raise Exception(f'Error while testing user interface: {str(ex)}')

Someone can help here to find out why this error is raised and how to avoid if possible and if not then how to caught Exception in generic block of same file i.e. here test.py or even any other solution that helps here.

Upvotes: 0

Views: 670

Answers (1)

politinsa
politinsa

Reputation: 3720

When webdriver.Chrome(webdriver_manager.chrome.ChromeDriverManager().install(), chrome_opt=chrome_opt) has an exception, no value is assigned to browser. The variable is hence never declared and does not exist.

Set it to None before the try block

    import webdriver_manager.chrome
    from selenium import webdriver

    browser = None
    try:         
       chrome_opt = webdriver.ChromeOptions()
       chrome_opt.add_argument('--headless')
       chrome_opt.add_argument('--no-sandbox')
       # raise Exception(f'inside test error: for re-producing above error')
       browser = webdriver.Chrome(webdriver_manager.chrome.ChromeDriverManager().install(),
                               chrome_opt=chrome_opt)
       # some e2e test-cases

    except TimeoutException as ex:
        if browser is not None: browser.quit()
        raise Exception(f'Time-out exception {str(ex)}')
    except Exception as ex:
        if browser is not None: browser.quit()
        raise Exception(f'Error while testing user interface: {str(ex)}')

Upvotes: 1

Related Questions