You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium调用Firefox遇执行文件错误,snap安装Firefox失败求助

解决Selenium调用Firefox及Snap安装失败问题

一、修复Snap服务无法连接的问题

Snap服务未正常运行是无法安装Firefox的根源,按以下步骤操作:

  • 检查Snap服务状态:
    sudo systemctl status snapd
    
  • 如果服务未启动,启动服务:
    sudo systemctl start snapd
    
  • 设置服务开机自启,避免重启后失效:
    sudo systemctl enable snapd
    
  • 重启服务确保配置生效:
    sudo systemctl restart snapd
    

二、安装Firefox Snap包

服务修复后,执行以下命令安装Firefox:

sudo snap install firefox

三、确认Firefox实际二进制路径

Snap安装的Firefox路径并非/usr/bin/firefox,执行以下命令获取正确路径:

which firefox

通常返回/snap/bin/firefox;若需要更精确的二进制文件路径,可执行:

find /snap/firefox -name firefox -type f

一般会得到类似/snap/firefox/current/usr/lib/firefox/firefox的结果。

四、修正Selenium脚本

针对脚本中的问题,做以下三处修改:

  1. 更新Firefox二进制路径:
    将原代码中的:

    firefox_options.binary_location = '/usr/bin/firefox'
    

    替换为你获取到的实际路径,例如:

    firefox_options.binary_location = '/snap/bin/firefox'
    
  2. 替换已弃用的元素查找方法:
    Selenium 4.x中find_elements_by_css_selector已被弃用,需先导入By模块:

    from selenium.webdriver.common.by import By
    

    再替换元素查找代码:

    header_nav_elements = driver.find_elements(By.CSS_SELECTOR, '.navbar-nav .nav-link')
    
  3. 修正等待逻辑:
    driver.implicitly_wait(5)是设置隐式等待超时时间,并非主动等待5秒,若需要等待页面加载,改用time.sleep():
    先导入time模块:

    import time
    

    再替换等待代码:

    time.sleep(5)
    

修正后的完整脚本示例:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.by import By
import json
import time

# create a Firefox service and options object
firefox_service = FirefoxService(executable_path='/usr/local/bin/geckodriver')
firefox_options = FirefoxOptions()

# set the binary location for Firefox (替换为你的实际路径)
firefox_options.binary_location = '/snap/bin/firefox'

# enable network logging
firefox_options.set_preference("devtools.netmonitor.har.enableAutoExportToFile", True)
firefox_options.set_preference("devtools.netmonitor.har.autoConnect", True)
firefox_options.set_preference("devtools.netmonitor.har.forceExport", True)
firefox_options.set_preference("devtools.netmonitor.har.fileName", "network_logs")

# create a Firefox driver instance with the specified service and options
driver = webdriver.Firefox(service=firefox_service, options=firefox_options)

# navigate to the lambdatest website
driver.get("https://www.lambdatest.com/")

# find the header navigation elements and click on each one
header_nav_elements = driver.find_elements(By.CSS_SELECTOR, '.navbar-nav .nav-link')
for element in header_nav_elements:
    element.click()
    # 每次点击后短暂等待,避免页面未加载完成
    time.sleep(1)

# wait for 5 seconds
time.sleep(5)

# close the browser window and quit the driver
driver.close()
driver.quit()

# read the network logs from the HAR file
with open('network_logs.har', 'r') as f:
    network_logs = json.load(f)

# print the network logs
print(json.dumps(network_logs, indent=2))

内容的提问来源于stack exchange,提问作者Chukwuneku

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 17:15:33