使用selenium调用firefox提示Profile Missing的问题解决

在Ubuntu22.04环境中,使用python3运行selenium提示 Profile Missing, 具体信息为:
Your Firefox profile cannot be loaded. It may be missing or inaccessible

在这个问题的环境中firefox浏览器工作正常。

排查中,手动在命令行执行firefox可以打开浏览器,但是出现如下提示

Gtk-Message: 15:32:09.944: Not loading module "atk-bridge": The functionality is provided by GTK natively. Please try to not load it.
[27589, Main Thread] WARNING: GTK+ module /snap/firefox/4136/gnome-platform/usr/lib/gtk-2.0/modules/libcanberra-gtk-module.so cannot be loaded.
GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported.: 'glib warning', file /build/firefox/parts/firefox/build/toolkit/xre/nsSigHandlers.cpp:187

(firefox:27589): Gtk-WARNING **: 15:32:09.987: GTK+ module /snap/firefox/4136/gnome-platform/usr/lib/gtk-2.0/modules/libcanberra-gtk-module.so cannot be loaded.
GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported.
Gtk-Message: 15:32:09.987: Failed to load module "canberra-gtk-module"
[27589, Main Thread] WARNING: GTK+ module /snap/firefox/4136/gnome-platform/usr/lib/gtk-2.0/modules/libcanberra-gtk-module.so cannot be loaded.
GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported.: 'glib warning', file /build/firefox/parts/firefox/build/toolkit/xre/nsSigHandlers.cpp:187

(firefox:27589): Gtk-WARNING **: 15:32:09.988: GTK+ module /snap/firefox/4136/gnome-platform/usr/lib/gtk-2.0/modules/libcanberra-gtk-module.so cannot be loaded.
GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported.
Gtk-Message: 15:32:09.988: Failed to load module "canberra-gtk-module"

事实证明,在这个环境中使用apt-get install libcanberra-gtk-module命令来安装软件包并不能解决问题。

主要的原因在于:
自Ubuntu 22.04开始,Firefox可能默认以Snap包形式提供,对于Snap版本的Firefox,配置文件路径通常位于:/home/$USER/snap/firefox/common/.mozilla/firefox/ 这个目录下,因此相关的代码变更如下:
原代码:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")

更新后的代码

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver import Firefox

 firefox_options = Options()
 firefox_options.add_argument('-profile')
 firefox_options.add_argument('/home/xxx/snap/firefox/common/.mozilla/firefox')
 driver = webdriver.Firefox(options=firefox_options)
driver.get("http://www.baidu.com")

特此记录一下

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用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使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值