【问题标题】:Selenium4 and Snap install of Firefox with Geckodriver_v31 times outSelenium 4 和使用 Geckodriver V31 快速安装 Firefox 超时
【发布时间】:2022-10-25 10:05:30
【问题描述】:

我最近升级了 lubuntu 22.04,它希望从 snap 存储库中安装一些东西。火狐就是其中之一。目前我正在使用 Selenium 4.1.3、Python 3.10 和 Firefox 99.0.1 以及最新的 geckodriver V31.0

我一直在使用这个 python3 代码进行测试,但现在它完全无法启动。

首先它没有找到一个配置文件,所以我在里面强制了一些东西:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

options = Options()
options.add_argument("-profile /path2temp/")  # create profile
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting",
                       False)
options.set_preference("browser.download.dir", "./data_export")
options.set_preference(
    "browser.helperApps.neverAsk.saveToDisk",
    "application/vnd.google-earth.kml+xml,application/x-zip-compressed,application/gpx+xml,text/csv"
)
options.set_preference("devtools.debugger.remote-enabled", True)
options.set_preference("devtools.debugger.prompt-connection", False)

browser = webdriver.Firefox(options=options, executable_path=r"/usr/bin/geckodriver")

url = 'https://cnn.com'
browser.get(url)

如果 Firefox 已打开,则无法与其通信。通常在过去它只会打开一个新标签并开始工作。但现在我得到这个错误:

Firefox 已经在运行,但没有响应。要使用 Firefox,您 必须首先关闭现有的 Firefox 进程,重新启动您的设备,或者 使用不同的配置文件。

如果我让它启动应用程序,它会在很长时间后超时并出现以下错误(注意,/path2temp/ 是它具有权限的目录的真实路径)。

1651528082918   geckodriver     
INFO    Listening on 127.0.0.1:54985 1651528083062   mozrunner::runner       
INFO    Running command: "/snap/bin/firefox" "--marionette" "-profile /path2temp/" "--remote-debugging-port" "47927" "-- remote-allow-hosts" "localhost" "-no-remote" 
ATTENTION: default value of option mesa_glthread overridden by environment. 
ATTENTION: default value of option mesa_glthread overridden by environment. 
ATTENTION: default value of option mesa_glthread overridden by environment. 
ATTENTION: default value of option mesa_glthread overridden by environment. 
DevTools listening on ws://localhost:47927/devtools/browser/19a59834-6a4b-4d75-902c-06c36704d50e 
Exiting due to channel error. 
Exiting due to channel error. 
Exiting due to channel error. 
Exiting due to channel error. 
Exiting due to channel error.

关于我可以做些什么来解决这个问题的任何想法?


编辑:通过将它传递给位于快照文件结构/home/username/snap/firefox/common/.mozilla/firefox/wnrrbapq.default-中的当前用户配置文件,我至少能够在它启动firefox时让它工作发布

但这不是一个理想的行为,因为我每次都必须关闭浏览器进行测试。

【问题讨论】:

    标签: selenium-webdriver firefox


    【解决方案1】:

    Snap 版本的 Firefox 无法写入 /tmp 位置。 推荐的解决方法是将TMPDIR 环境变量设置为geckodriverfirefox 都可以写入的位置,例如$HOME/tmp

    geckdriver 0.31.0 relaese notesrelated github issue 中的更多信息。

    【讨论】:

      【解决方案2】:

      一种解决方案是根本不设置配置文件,而是为firefoxgeckodriver 定义文件路径。

      由于 Snap 喜欢进行一致的更新,我发现使我的文件路径动态化很有帮助。这是一些示例代码:

      from selenium import webdriver
      from selenium.webdriver.firefox.options import Options
      from selenium.webdriver.firefox.service import Service
      from subprocess import getoutput
      
      options = Options()
      options.binary_location = getoutput("find /snap/firefox -name firefox").split("
      ")[-1]
      
      driver = webdriver.Firefox(service =
          Service(executable_path = getoutput("find /snap/firefox -name geckodriver").split("
      ")[-1]),
          options = options)
      url = 'https://cnn.com'
      driver.get(url)
      

      您可以使用getoutput 获取/snap/firefox 中的文件列表,其中包含1 个长字符串。根据 拆分它们并获取[-1](最后一个)记录可为您提供该文件的最新版本。

      您可能不希望始终使用最新版本,因此如果您需要一致性,您总是可以选择列表中的第一条记录。虽然很高兴 Snap 将同时更新 geckodriver 和 Firefox,因此它们应该始终一起工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-30
        • 2021-11-02
        • 2019-08-30
        • 2020-06-04
        • 2018-03-31
        • 2017-05-24
        相关资源
        最近更新 更多