我正在构建我的第一个 Flask 应用程序,但在集成 selenium 时遇到问题。该应用程序从 html 文本字段获取输入,使用它通过 selenium 传递到搜索字段,然后运行一些代码。但是,在文本字段中输入内容并点击提交后,我收到以下错误:
An error occurred in get_lyrics: Message: Service /home/vsherr/.cache/selenium/chromedriver/linux64/120.0.6099.109/chromedriver unexpectedly exited. Status code was: 127
An error occurred: Message: Service /home/vsherr/.cache/selenium/chromedriver/linux64/120.0.6099.109/chromedriver unexpectedly exited. Status code was: 127
[2023-12-29 12:30:52,471] ERROR in app: Exception on /search [POST]
Traceback (most recent call last):
File "/mnt/c/Users/vsher/Desktop/misc-coding/lyyric/search.py", line 19, in search
word_list = get_lyrics(song)
File "/mnt/c/Users/vsher/Desktop/misc-coding/lyyric/search.py", line 34, in get_lyrics
driver = webdriver.Chrome(options=options)
File "/home/vsherr/.local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
super().__init__(
File "/home/vsherr/.local/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 50, in __init__
self.service.start()
File "/home/vsherr/.local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 102, in start
self.assert_process_still_running()
File "/home/vsherr/.local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 115, in assert_process_still_running
raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}")
selenium.common.exceptions.WebDriverException: Message: Service /home/vsherr/.cache/selenium/chromedriver/linux64/120.0.6099.109/chromedriver unexpectedly exited. Status code was: 127
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/vsherr/.local/lib/python3.10/site-packages/flask/app.py", line 1455, in wsgi_app
response = self.full_dispatch_request()
File "/home/vsherr/.local/lib/python3.10/site-packages/flask/app.py", line 869, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/vsherr/.local/lib/python3.10/site-packages/flask/app.py", line 867, in full_dispatch_request
rv = self.dispatch_request()
File "/home/vsherr/.local/lib/python3.10/site-packages/flask/app.py", line 852, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/mnt/c/Users/vsher/Desktop/misc-coding/lyyric/search.py", line 25, in search
return render_template('error.html', error_message=str(e))
File "/home/vsherr/.local/lib/python3.10/site-packages/flask/templating.py", line 151, in render_template
template = app.jinja_env.get_or_select_template(template_name_or_list)
File "/home/vsherr/.local/lib/python3.10/site-packages/jinja2/environment.py", line 1081, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/home/vsherr/.local/lib/python3.10/site-packages/jinja2/environment.py", line 1010, in get_template
return self._load_template(name, globals)
File "/home/vsherr/.local/lib/python3.10/site-packages/jinja2/environment.py", line 969, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "/home/vsherr/.local/lib/python3.10/site-packages/jinja2/loaders.py", line 126, in load
source, filename, uptodate = self.get_source(environment, name)
File "/home/vsherr/.local/lib/python3.10/site-packages/flask/templating.py", line 65, in get_source
return self._get_source_fast(environment, template)
File "/home/vsherr/.local/lib/python3.10/site-packages/flask/templating.py", line 99, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: error.html
127.0.0.1 - - [29/Dec/2023 12:30:52] "POST /search HTTP/1.1" 500 -
我尝试升级selenium并安装许多软件包,包括:
pip install -U selenium
pip install --upgrade chromedriver-autoinstaller
sudo apt-get install -y chromium-browser
可能是什么原因造成的?
编辑:我已经缩小了范围,但仍然没有解决问题。这与 chromedriver 在 Ubuntu 中无法工作有关。以前的解决方案涉及编辑文件路径以访问 chromedriver。然而,这个解决方案已经过时了,因为 chromedriver 不再需要文件路径。
我修好了!与损坏的依赖/安装有关。使用 pip install dependency 解决了第一个问题。然后我使用
sudo apt --fix-broken install
来修复所有这些安装。现在完美运行。