0


Linux系统使用Selenium调用Firefox浏览器遇到报错

一、报错

InvalidArgumentException Traceback (most recent call last)

/tmp/ipykernel_8681/3420186475.py in <cell line: 2>()

  1. 1 # 初始化 WebDriver 对象

----> 2 driver = webdriver.Firefox(service=service, options=options)

/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py in init(self, options, service, keep_alive)

  1. 69
  2. 70 try:

---> 71 super().init(command_executor=executor, options=options)

  1. 72 except Exception:
  2. 73 self.quit()

/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py in init(self, command_executor, keep_alive, file_detector, options)

  1. 210 self._authenticator_id = None
  2. 211 self.start_client()

--> 212 self.start_session(capabilities)

  1. 213
  2. 214 self._websocket_connection = None

/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities)

  1. 297
  2. 298 caps = _create_caps(capabilities)

--> 299 response = self.execute(Command.NEW_SESSION, caps)["value"]

  1. 300 self.session_id = response.get("sessionId")
  2. 301 self.caps = response.get("capabilities")

/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)

  1. 352 response = self.command_executor.execute(driver_command, params)
  2. 353 if response:

--> 354 self.error_handler.check_response(response)

  1. 355 response["value"] = self._unwrap_value(response.get("value", None))
  2. 356 return response

/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)

  1. 227 alert_text = value["alert"].get("text")
  2. 228 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here

--> 229 raise exception_class(message, screen, stacktrace)

InvalidArgumentException: Message: binary is not a Firefox executable

driver = webdriver.Firefox()
Traceback (most recent call last):
File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/common/driver_finder.py", line 67, in _binary_paths
output = SeleniumManager().binary_paths(self._to_args())
File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/common/selenium_manager.py", line 46, in binary_paths
args = [str(self._get_binary())] + args
File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/common/selenium_manager.py", line 91, in _get_binary
raise WebDriverException(f"Unsupported platform/architecture combination: {sys.platform}/{arch}")
selenium.common.exceptions.WebDriverException: Message: Unsupported platform/architecture combination: linux/aarch64

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 57, in init
if finder.get_browser_path():
File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/common/driver_finder.py", line 47, in get_browser_path
return self._binary_paths()["browser_path"]
File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/common/driver_finder.py", line 78, in _binary_paths
raise NoSuchDriverException(msg) from err
selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for firefox; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

大概率是geckodriver没装好,我就是没安装好,弄了好久,最后从官网下载了 geckodriver压缩包,把解压后的geckodriver放到/usr/bin路径下才解决

raceback(mostrecentcalllast): File "<stdin>",line 1,in <module> File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py",line 7l,ininitsuper(),init (command executor=executor,options=options)File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py",line 212,in initself.start session(capabilities)File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py",line 299,in start sessionresponse =self.execute(Command.NEW SESSION, caps)["value"]File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py",line 354,in executeself.error handler.check response(response)File "/usr/local/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py"line 229,in check response raise exception cass(message,screen,stacktrace)selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1

这种也要检查自己指定的firefox和geckodriver是否正确

firefox可以直接在命令行中输入文件路径,如果浏览器正常打开那么路径没有问题,可能遇到权限不够的情况

如果遇到Permission denied,多半是权限不够

使用 ls -l /usr/lib/firefox(你的受限制文件路径) 命令,查看文件的权限

然后使用 chmod +x /usr/lib/firefox(你的受限制文件路径) 命令为用户添加执行权限

然后再输入firefox路径

查看自己firefox的位置

whereis firefox或者

which firefox 或者

sudo find / -type f -name 'firefox'查找所有可能的位置

二、兼容情况

还有可能有兼容情况的问题

下面的表官网上给出的是geckodriver,Selenium和Firefox的版本支持情况

geckodriver

Selenium

Firefox

min

max

0.34.0

≥ 3.11 (3.14 Python)

115 ESR

n/a

0.33.0

≥ 3.11 (3.14 Python)

102 ESR

120

0.32.2

≥ 3.11 (3.14 Python)

102 ESR

120

0.32.1

≥ 3.11 (3.14 Python)

102 ESR

120

0.32.0

≥ 3.11 (3.14 Python)

102 ESR

120

0.31.0

≥ 3.11 (3.14 Python)

91 ESR

120

0.30.0

≥ 3.11 (3.14 Python)

78 ESR

90

0.29.1

≥ 3.11 (3.14 Python)

60

90

0.29.0

≥ 3.11 (3.14 Python)

60

90

0.28.0

≥ 3.11 (3.14 Python)

60

90

0.27.0

≥ 3.11 (3.14 Python)

60

90

0.26.0

≥ 3.11 (3.14 Python)

60

90

0.25.0

≥ 3.11 (3.14 Python)

57

90

0.24.0

≥ 3.11 (3.14 Python)

57

79

0.23.0

≥ 3.11 (3.14 Python)

57

79

0.22.0

≥ 3.11 (3.14 Python)

57

79

0.21.0

≥ 3.11 (3.14 Python)

57

79

0.20.1

≥ 3.5

55

62

0.20.0

≥ 3.5

55

62

0.19.1

≥ 3.5

55

62

0.19.0

≥ 3.5

55

62

0.18.0

≥ 3.4

53

62

0.17.0

≥ 3.4

52

62

查看自己的geckodriver,Selenium,Firefox,python版本

  1. geckodriver完整地址 --version
  2. firefox --version
  3. python --version
  4. pip show selenium

selenium版本不匹配需要使用pip/conda uninstallselenium卸载掉,然后重新pip install

geckodrive版本不匹配就删掉,去官网下载适合的版本

我使用的firefox是系统自带的

火狐浏览器驱动geckodriver的官方下载地址

Releases · mozilla/geckodriver (github.com)

因为我用的是Linux系统,所以我下载的压缩包是

geckodriver-v0.34.0-linux-aarch64.tar.gz

解压两次之后就是我们需要的geckodriver文件,然后把他放再系统的/usr/bin目录下,同一路径中还有firefox的链接,原始firefox的链接可能连接着firefox.sh

可以用指令rm -rf /usr/bin/firefox删除原始链接,然后ln -s /usr/lib/firefox/firefox /usr/bin/firefox
创建新命令

三、代码

使用Selenium和指定的firefox和geckodriver调用浏览器,python代码:

  1. from selenium import webdriver
  2. from selenium.webdriver.firefox.options import Options
  3. from selenium.webdriver.firefox.service import Service
  4. # 设置 Firefox 可执行文件的路径
  5. firefox_path = '/path/to/your/firefox'
  6. # 设置 GeckoDriver 的路径
  7. geckodriver_path = '/path/to/your/geckodriver'
  8. # 创建 Firefox 选项对象,并指定浏览器路径
  9. options = Options()
  10. options.binary_location = firefox_path # 使用 binary_location 指定 Firefox 路径
  11. service = Service(executable_path=geckodriver_path)
  12. # 创建 GeckoDriver 服务对象,并指定驱动路径
  13. driver = webdriver.Firefox(service=service, options=options)
  14. # 打开百度首页测试是否正常
  15. driver.get("http://www.baidu.com")

参考

Linux配置Python+Selenium+Firefox_python的火狐浏览器驱动selenium linux 有图形化界面-CSDN博客

标签: linux selenium firefox

本文转载自: https://blog.csdn.net/myhfory/article/details/138360205
版权归原作者 myhfory 所有, 如有侵权,请联系我们删除。

“Linux系统使用Selenium调用Firefox浏览器遇到报错”的评论:

还没有评论