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

一、报错 

InvalidArgumentException                  Traceback (most recent call last)

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

      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)

     69

     70         try:

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

     72         except Exception:

     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)

    210         self._authenticator_id = None

    211         self.start_client()

--> 212         self.start_session(capabilities)

    213

    214         self._websocket_connection = None

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

    297

    298         caps = _create_caps(capabilities)

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

    300         self.session_id = response.get("sessionId")

    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)

    352         response = self.command_executor.execute(driver_command, params)

    353         if response:

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

    355             response["value"] = self._unwrap_value(response.get("value", None))

    356             return response

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

    227                 alert_text = value["alert"].get("text")

    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版本

geckodriver完整地址 --version
firefox --version
python --version
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代码:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service

# 设置 Firefox 可执行文件的路径
firefox_path = '/path/to/your/firefox'

# 设置 GeckoDriver 的路径
geckodriver_path = '/path/to/your/geckodriver'

# 创建 Firefox 选项对象,并指定浏览器路径
options = Options()
options.binary_location = firefox_path  # 使用 binary_location 指定 Firefox 路径
service = Service(executable_path=geckodriver_path)

# 创建 GeckoDriver 服务对象,并指定驱动路径
driver = webdriver.Firefox(service=service, options=options)
# 打开百度首页测试是否正常
driver.get("http://www.baidu.com")

参考

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

  • 19
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Selenium调用Firefox浏览器,首先需要安装一些必要的组件和驱动程序。下面是一些步骤可以帮助你实现这一目标: 1. 首先,确保你已经安装了Firefox浏览器和Python。 2. 下载Geckodriver,这是Firefox浏览器的驱动程序。你可以在https://github.com/mozilla/geckodriver/releases找到可用的版本。根据你的操作系统,下载适当的版本并将其解压到任意文件夹中。 3. 在Python脚本中导入selenium库: ``` from selenium import webdriver ``` 4. 指定Geckodriver的路径: ``` driver_path = r'D:\迅雷下载\driver\geckodriver.exe' ``` 5. 创建Firefox浏览器的实例: ``` driver = webdriver.Firefox(executable_path=driver_path) ``` 6. 使用`get()`方法打开一个网页: ``` driver.get('https://baidu.com') ``` 这将在Firefox浏览器中打开百度网页。 7. 最后,不要忘记关闭浏览器会话,以释放资源: ``` driver.quit() ``` 这些步骤应该能帮助你成功地使用Selenium调用Firefox浏览器。请确保你已按照上述步骤正确设置了Geckodriver的路径,并且已经安装了selenium库。如果你遇到了其他问题,可以查阅Selenium文档或在社区寻求帮助[1]。 : 本来在使用selenium调用谷歌浏览器的时候,因为一个网站的原因,一直会自动弹出网页的通知,如下: : 主要介绍了python selenium firefox使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值