我正在尝试在 Ubuntu 23.04(最新版本)中使用 selenium(python)运行一个基本脚本。
因为我无法在 ubuntu 23.04 中使用 Pip。当我尝试使用 pip 安装selenium 和 webdriver 管理器然后显示此错误:
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.
If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.
If you wish to install a non-Debian packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.
See /usr/share/doc/python3.11/README.venv for more information.
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
然后我尝试使用虚拟环境。我可以在 Venv 中安装 selenium 和 Webdriver Manager。但随后我遇到了一个新问题,我编写了一个基本脚本火狐(Selenium-python)和 Webdriver 管理器:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
driver.get("https://www.rokomari.com")
print(driver.title)
driver.close()
然后显示错误:
Traceback (most recent call last):
File "/media/zahed/Project_Life/Software_Testing/Selenium/test.py", line 5, in <module>
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/zahed/Project_Life/Software_Testing/Selenium/.venv/lib/python3.11/site-packages/selenium/webdriver/firefox/webdriver.py", line 201, in __init__
super().__init__(command_executor=executor, options=options, keep_alive=True)
File "/media/zahed/Project_Life/Software_Testing/Selenium/.venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
self.start_session(capabilities, browser_profile)
File "/media/zahed/Project_Life/Software_Testing/Selenium/.venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/zahed/Project_Life/Software_Testing/Selenium/.venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "/media/zahed/Project_Life/Software_Testing/Selenium/.venv/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1
还尝试使用 Chromium WebDriver:然后 Choromium 浏览器就会打开,但不会加载网站。只显示空白屏幕铬脚本
现在,有人可以帮助我如何在 ubuntu 23.04 中为 chrome 和 Firefox 实际运行 selenium 脚本。
提前致谢。