UnboundLocalError from Splinter?



我安装了Firefox并使用Ubuntu 18.04。

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

结果:

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

我不知道如何解决这个问题。我检查了Splinter的文档,但没有此错误的提示。

我做错了什么?

更新字符串库后:

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. 

终于成功了!我不得不从 Github 更新 Splinter 库并将 Geckodriver 文件放入/usr/bin 中

首先,通过转到"帮助-关于谷歌浏览器"来检查您的 chrome 版本。

其次,转到 https://chromedriver.chromium.org/并下载与您当前谷歌浏览器匹配的版本。

第三,然后将Chromedriver放入您的垃圾箱。

对于Mac用户,打开Finder,然后执行shift+command+G,然后键入"/usr/local/bin/">

我遇到了同样的问题。我意识到 splinter 的 pip 安装在 splinter/browser.py 文件中的 get_broswer(( 函数中存在错误。

PIP安装版本的碎片,给出了错误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版本:

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

更新到 GitHub 版本后,我能够找到真正的根本原因问题,即我使用的是旧版本的设置 chromedriver。我在这里为这个问题找到了一个很好的解决方案。

在此解决方案中,Navarasu建议您pip install webdriver-manager,然后您可以按如下方式调用浏览器:

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

类似的方法也可以用于 Firefox。

我遇到了同样的问题。我恢复到分裂-0.11.0。

要扩展 Windows 的最后一个问题,请执行以下操作: 下载相应的 geckodriver (https://github.com/mozilla/geckodriver/releases(,并将 geckodriver.exe 文件放入 PATH 变量引用的位置。

在Windows 10上使用Chrome,我进行了两次编辑,但不确定哪一次解决了错误。

我降级了我的 chrome 驱动程序版本(比当前版本低一个(。 我添加了这行代码。

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

相关内容

  • 没有找到相关文章

最新更新