Lapinski
Lapinski

Reputation: 31

UnboundLocalError from Splinter?

I installed Firefox and using Ubuntu 18.04.

from splinter import Browser

with Browser() as browser:
    # Visit URL
    url = "http://www.google.com"
    browser.visit(url)

Results in:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    with Browser() as browser:
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/splinter/browser.py", line 90, in Browser
    return get_driver(driver, *args, **kwargs)
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/splinter/browser.py", line 68, in get_driver
    raise e
UnboundLocalError: local variable 'e' referenced before assignment
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    with Browser() as browser:
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/splinter/browser.py", line 90, in Browser
    return get_driver(driver, *args, **kwargs)
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/splinter/browser.py", line 68, in get_driver
    raise e
UnboundLocalError: local variable 'e' referenced before assignment
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    with Browser() as browser:
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/splinter/browser.py", line 90, in Browser
    return get_driver(driver, *args, **kwargs)
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/splinter/browser.py", line 68, in get_driver
    raise e
UnboundLocalError: local variable 'e' referenced before assignment

I'm not sure how to solve this problem. I checked the documentation from Splinter but there is no hint for this error.

What I am doing wrong ?

After updating the Stringer Lib:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    with Browser() as browser:
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/splinter/browser.py", line 92, in Browser
    return get_driver(driver, *args, **kwargs)
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/splinter/browser.py", line 70, in get_driver
    raise err
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/splinter/browser.py", line 66, in get_driver
    return driver(*args, **kwargs)
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/splinter/driver/webdriver/firefox.py", line 88, in __init__
    **kwargs
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
    self.service.start()
  File "/home/sebastian/PycharmProjects/SupremeBot/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

Upvotes: 2

Views: 3865

Answers (5)

mcbridecaleb
mcbridecaleb

Reputation: 101

Using Chrome on Windows 10, I made two edits and I'm not sure which one solved the error.

I downgraded my chrome driver version (to one lower than my current version). I added this line of code.

executable_path = {'executable_path': '/usr/bin/chromedriver'}
browser = Browser('chrome', **executable_path, headless=False)

Upvotes: 0

Lemon
Lemon

Reputation: 19

I had the same issue. I realized that the pip install of splinter had a bug in the get_broswer() function in the splinter/browser.py file.

pip install version of splinter that gave error UnboundLocalError: local variable 'e' referenced before assignment:

def get_driver(driver, retry_count=3, *args, **kwargs):
"""Try to instantiate the driver.

Common selenium errors are caught and a retry attempt occurs.
This can mitigate issues running on Remote WebDriver.

"""

    for _ in range(retry_count):
        try:
            return driver(*args, **kwargs)
    except (IOError, HTTPException, WebDriverException, MaxRetryError) as e:
        pass

    raise e

GitHub version:

def get_driver(driver, retry_count=3, *args, **kwargs):
"""Try to instantiate the driver.

Common selenium errors are caught and a retry attempt occurs.
This can mitigate issues running on Remote WebDriver.

"""
    err = None
    for _ in range(retry_count):
        try:
            return driver(*args, **kwargs)
        except (IOError, HTTPException, WebDriverException, MaxRetryError) as e:
            err = e

    raise err

After updating to the GitHub version, I was able to find the real root cause problem, which was that I was using an older version of setting up chromedriver. I found a good solution here for that issue.

In this solution, Navarasu suggests you pip install webdriver-manager, then you can call your browser as follows:

from splinter import Browser
from webdriver_manager.chrome import ChromeDriverManager

executable_path = {'executable_path': ChromeDriverManager().install()}
browser = Browser('chrome', **executable_path)

A similar approach can be used for Firefox.

Upvotes: 1

mimivi
mimivi

Reputation: 11

First, check your chrome version, by going to "Help-About Google Chrome".

Second, go to https://chromedriver.chromium.org/ AND download the version that matched your current Google Chrome.

Third, then put the Chromedriver into your bin.

For Mac-users, open Finder, then do shift+command+G, and then type "/usr/local/bin/"

Upvotes: 1

Panda Mawr
Panda Mawr

Reputation: 1

I had the same issues. I reverted back to splinter-0.11.0.

To expand on the last issue for Windows: Download the appropriate geckodriver (https://github.com/mozilla/geckodriver/releases) and put the geckodriver.exe file into the location your PATH variable is referring to.

Upvotes: 0

Lapinski
Lapinski

Reputation: 31

Finally it works ! I had to update the Splinter lib from Github and put the Geckodriver file into the /usr/bin

Upvotes: 1

Related Questions