为什么我不能在 Ubuntu 22.04 上使用 Selenium/Javascript 控制 Firefox,而我可以使用 Python?

问题描述 投票:0回答:1

我有一个Python脚本来使用selenium控制firefox,它工作得很好:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get("https://dev.to")
 
driver.find_element(By.CLASS_NAME, "crayons-header--search-input").send_keys("Selenium")

我尝试使用 javascript 做同样的事情,并收到错误“二进制文件不是 Firefox 可执行文件”

这是当前的 JavaScript:

const { Builder, By, Key, until } = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');

(async function example() {
  // Define the path to the Firefox binary
  // const firefoxOptions = new firefox.Options().setBinary('/usr/bin/firefox');
  const firefoxOptions = new firefox.Options().setBinary('/real_path_here/firefox/firefox');

  // Create a new WebDriver instance with Firefox
  const driver = await new Builder().forBrowser('firefox').setFirefoxOptions(firefoxOptions).build();

  try {
    // Navigate to the website
    await driver.get('https://dev.to');

    const searchInput = await driver.findElement(By.className('crayons-header--search-input'));
    await searchInput.sendKeys('Selenium', Key.RETURN);
    await driver.wait(until.titleIs('Search Results - DEV Community'), 10000);

  } finally {
    await driver.quit();
  }
})();

当我使用节点运行此命令时,我收到此错误消息:

Selenium Manager binary found at /real_path_here/node_modules/selenium-webdriver/bin/linux/selenium-manager
Driver path: /snap/bin/geckodriver
Browser path: /home/sumofchemicals/.cache/selenium/firefox/linux64/118.0.1/firefox
/real_path_here/node_modules/selenium-webdriver/lib/error.js:524
    let err = new ctor(data.message)
              ^

InvalidArgumentError: binary is not a Firefox executable
    at Object.throwDecodedError (/real_path_here/node_modules/selenium-webdriver/lib/error.js:524:15)
    at parseHttpResponse (/real_path_here/node_modules/selenium-webdriver/lib/http.js:601:13)
    at Executor.execute (/real_path_here/node_modules/selenium-webdriver/lib/http.js:529:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  remoteStacktrace: ''
}

Node.js v18.15.0

自从这些文档专门讨论 Ubuntu 22-04 上的 snap/flatpak 问题以来,我已经尝试设置二进制路径。

在我共享的示例代码中,由于假定的 snap/flatpak 问题,我下载了一个单独的 Firefox 副本,并尝试在我的系统上指定该路径。错误消息不反映该路径,而是提供了提及 .cache 的不同浏览器路径。如果我将路径指定为 /usr/bin/firefox,错误消息将镜像该路径,但仍然不起作用。

javascript selenium-webdriver firefox geckodriver ubuntu-22.04
1个回答
0
投票

我能够做到,但仍然不完全理解它为什么有效。

按照有人的建议,我将 geckodriver 的副本移至 /usr/local/bin here。它仍然不起作用,但收到一条新的错误消息,说明我尚未创建配置文件。

然后,我将非 snap firefox 的副本也移动到 /usr/local/bin,并指定二进制文件的路径:

  const firefoxOptions = new firefox.Options().setBinary('/usr/local/bin/firefox');

  // Create a new WebDriver instance with Firefox
  const driver = await new Builder().forBrowser('firefox').setFirefoxOptions(firefoxOptions).build();

然后就可以了。尽管它仍然在终端中列出了二进制文件的不同路径。我也很想让它与我的日常版本的 Firefox 而不是这个副本一起使用。

Driver path: /usr/local/bin/geckodriver
Browser path: /home/greg/.cache/selenium/firefox/linux64/118.0.1/firefox
© www.soinside.com 2019 - 2024. All rights reserved.