使用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")

特此记录一下

最近更新

  1. TCP协议是安全的吗?

    2024-04-23 13:40:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-23 13:40:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-23 13:40:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-23 13:40:03       20 阅读

热门阅读

  1. 【前端】vue.config.js打包时不编译

    2024-04-23 13:40:03       17 阅读
  2. vue中如何控制一个全局接口的调用频率

    2024-04-23 13:40:03       20 阅读
  3. ui_admin_vue3启动

    2024-04-23 13:40:03       13 阅读
  4. 图片 组件 vue2+element

    2024-04-23 13:40:03       16 阅读
  5. 谈谈 vue 生命周期

    2024-04-23 13:40:03       14 阅读
  6. python输入输出特殊处理

    2024-04-23 13:40:03       14 阅读
  7. 单链表(详解)

    2024-04-23 13:40:03       10 阅读
  8. 腾讯云开通幻兽帕鲁服务器需要多少钱?30元

    2024-04-23 13:40:03       14 阅读
  9. 回归决策树的构建

    2024-04-23 13:40:03       14 阅读
  10. 【Camera Sensor Driver笔记】七、点亮指南之Flash

    2024-04-23 13:40:03       14 阅读
  11. 总结:IP地址、网络地址与子网掩码的理解

    2024-04-23 13:40:03       14 阅读