What is preventing me from using Selenium/Javascript to control Firefox on Ubuntu 22.04, when I am able to do so with Python?

My python script effectively controls Firefox using Selenium:

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

Now, I am attempting to achieve the same functionality using JavaScript, but I encounter an error stating "binary is not a Firefox executable."

Below is my current JavaScript code snippet:

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('/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();
  }
})();

Executing this in node.js results in the following error message:

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

I've made attempts to specify the binary path based on information from documentation concerning snap/flatpak issues specifically on Ubuntu 22-04.

The provided sample code involves downloading a separate version of Firefox due to the snap/flatpak issue and specifying its path on my system. However, the error message refers to a different browser path in the .cache directory rather than the specified path. Even when setting the path to /usr/bin/firefox, the error message reflects that change but still does not resolve the issue.

Answer №1

After some trial and error, I managed to make it work, although I'm still not completely clear on the underlying reasons.

Following a suggestion from someone on Stack Overflow here, I moved a copy of geckodriver to /usr/local/bin. However, this didn't solve the issue and instead threw an error message about missing profile data.

Next, I placed a non-snap version of Firefox in /usr/local/bin and provided that path as the binary:

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

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

This change made it work, despite the fact that the terminal still displayed a different binary path. Additionally, I aim to configure it with my regular Firefox setup rather than using this standalone version.

Driver path: /usr/local/bin/geckodriver
Browser path: /home/greg/.cache/selenium/firefox/linux64/118.0.1/firefox

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on including the angular value (id) in conjunction with repeating data-target for executing a function

I have the following code snippet. Now, I am trying to pass a dynamic AngularJS variable, specifically the id of a repeating list. The button in question is used for deactivation purposes, and upon clicking it, a confirmation box pops up. Upon confirming ...

Python's Selenium WebDriver seems to be malfunctioning when using `driver.find_element(By.XPATH, '')` syntax

Hoping to retrieve the Full Name "Aamer Jamal" using Selenium WebDriver from the provided URL above. However, encountering an issue where a NoSuchElementException is thrown. `Check out the code snippet below: from selenium import webdriver from selenium.we ...

Camera Capacitor designed to eliminate popup notifications

I am utilizing Angular along with the camera plugin in Capacitor to locally save images on both desktop and tablets. I aim to utilize the CameraSource to directly access the camera or open the gallery for files without displaying a prompt. This is how my ...

Leveraging Multiple MongoDB Databases in Meteor.js

Can 2 Meteor.Collections fetch data from separate MongoDB database servers? Dogs = Meteor.Collection('dogs') // mongodb://192.168.1.123:27017/dogs Cats = Meteor.Collection('cats') // mongodb://192.168.1.124:27017/cats ...

Jenkins error: Selenium SessionNotCreatedException caused by 'Insufficient space on device (OS error 28) for path "/tmp/rust_mozprofileO0WZJN"'

Encountered an error while trying to run Selenium using Jenkins: org.openqa.selenium.SessionNotCreatedException: Could not initiate a new session. Issue arose during session creation with the driver service, leading to service stoppage. Response code 500. ...

Filtering a Two-Dimensional Array Using JavaScript

I am working with a basic 2D array that contains x, y coordinates as shown below: var c = [ [1,10], [2,11], [3,12], [4,13], [5,15] ]; I want to know how I can extract pairs from the array that meet specific conditi ...

Issue encountered while attempting to send an HTML file using express.js

I have been trying to display an HTML file using Express.js with a static __dirname, but the HTML content does not show up after calling localhost/ var express = require('express'); var web = express(); ...

Error: "Access-Control-Allow-Origin" header is missing in Firebase Function

I have encountered an issue with my firebase functions GET request. While I am able to receive data using Postman, I am facing difficulties when trying to fetch data from my front-end application. Upon accessing the endpoints, I am seeing the following er ...

Locate the nearest index within the array

I'm currently working with an array of "events" where the key assigned to each event corresponds to the Unix Timestamp of that particular event. To illustrate, consider the following array of event objects in JS: var MyEventsArray=[]; MyEventsArray[1 ...

Is there a way to prevent an external script from making changes to an inline style?

There is a mysterious script running on a page that seems to be controlling the height of an inline style. The source of this script modifying the height property is unknown. <div class="vgca-iframe-wrapper wpfa-initialized" style="heigh ...

The jQuery .load function does not function properly when an ajax query is already underway

My web application utilizes dynamic loading of content within the same div (.content) through either an ajax request or a .load() request. An example of the ajax request code snippet is: $(document).on('click', '.button1', functio ...

How is UI Router Extras causing unexpected errors in my unit tests?

QUESTION: - I am facing failures in my tests after installing ui-router-extras. How can I resolve this issue? - Is there a way to use ui-router-extras without causing test failures? If you want to quickly install this, use yeoman along with angular-full ...

Challenges with variable scopes and passing variables in Ionic 2 (Typescript)

In my Ionic 2 TypeScript file, I am facing an issue with setting the value of a variable from another method. When I close the modal, I get undefined as the value. I'm encountering difficulty in setting the value for coord. export class RegisterMapP ...

Choosing elements in jQuery

Having some trouble with the subnav on my current website project. I think the issue lies in how I am selecting items in my jquery code. It seems like a small fix that needs to be made, but I'm unsure of the correct approach. http://jsfiddle.net/ZDEr ...

What is the process for reading server responses following the delivery of an alert to a designated user through socket.io?

Recently, I started exploring Socket.io and encountered an interesting challenge. My goal is to retrieve real-time location data from an Android device. To achieve this, I developed a basic application to access the current GPS coordinates. Additionally, I ...

Developing Angular dynamic components recursively can enhance the flexibility and inter

My goal is to construct a flexible component based on a Config. This component will parse the config recursively and generate the necessary components. However, an issue arises where the ngAfterViewInit() method is only being called twice. @Component({ ...

Node.js may not always save Winston log files when using process.exit()

I'm struggling to grasp a particular concept... when I use process.exit() in a program that logs events to both the console and a file, all the log events are displayed on the console as expected, but only the first one (or none at all) is actually sa ...

What is the solution for the error message "Unhandled Runtime Error" with the description "TypeError: videoRef.current.play is not a function"?

I am currently working on implementing custom controls for a video on a Nextjs website. When using a standard HTML <video> component, the code functions as expected and clicking the custom play button successfully plays the video. However, when I swi ...

The fetch API does not retain PHP session variables when fetching data

After setting session variables in one php file, I am attempting to access those values in another php file. session_start(); $_SESSION['user'] = $row['user']; $_SESSION['role'] = $row['role']; However, when ...

Dividing a select option

Looking to transform a single select element into multiple select elements using the separator "/" Here is an example of the original code: <select> <option value="1234">Type 1 / Black</option> <option value="5678">Type 2 / White& ...