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

Initiating, halting, and rejuvenating a timer in JavaScript

Here is a simple code snippet where I'm experimenting with starting, stopping, and resetting a JavaScript timer. The goal is to have a timer running on a page that sends a message once it reaches the end of its countdown before restarting. The stop b ...

Scrolling through a list of objects in a React component to display a vertical lineup of items including the name and logo

Currently, I am working on developing a vertical scrolling ticker/item list that showcases both the name and logo of each item within an array. Initially, I had set up a scrolling ticker solely with names using webkit animations on styled components. Howev ...

VueJS fails to display table information

I am facing an issue with rendering data from my API using VueJS 2. Although the backend services are successfully sending the data, the HTML table does not display it. There are no errors shown in the debug console, and even the Vue Debug extension for Fi ...

Leveraging async/await within a React functional component

Just getting started with React for a new project and facing challenges incorporating async/await functionality into one of my components. I've created an asynchronous function called fetchKey to retrieve an access key from an API served via AWS API ...

What would the equivalent Javascript implementation look like for this Python code that decodes a hex string and then encodes it to base64?

Currently, I am facing the challenge of transferring code from a Python script that decodes and encodes a string using a series of decode() and encode() functions. In Python, the code appears as follows: import codecs input = '3E061F00000E10FE' ...

Requesting data asynchronously using AJAX and displaying the filtered results on a webpage using JSON

When I send a request to my node.js server for a .json file containing person information (first name, last name), I want the data to be filtered based on user input. For example: When I request the .json file from the server, it gives me a list of people ...

Identifying Mistakes during Promise Initialization

Looking for a more efficient way to work with Bluebird promises Promise.resolve() .then(function() {return new MyObject(data)}) .then.....etc .catch(function (e){ //handle it}) I am dealing with MyObject and data coming from an external sourc ...

What is the best way to include variable child directives within a parent directive in AngularJS?

Exploring a New Angular Challenge In the current project I am engaged in, I am faced with a unique problem that requires an 'angular way' solution. Despite my best efforts, I seem to be hitting a roadblock every time I attempt to solve it. The ...

Dealing with Superagent and Fetch promises - Tips for managing them

Apologies for posing a question that may be simple for more seasoned JS programmers. I've been delving into superagent and fetch to make REST calls, as I successfully implemented odata but now need REST functionality. However, I'm facing confusio ...

I am receiving an undefined value when using document.getElementsByClassName

<canvas id="can" height="500px" width="1200px"></canvas> <div class="name"> <h1>LEONARDO</h1> </div> <script> var name=['WATSON','LEONARDO',"SMITH","EMILY"] var counter=0 var dat ...

I'm having trouble importing sqlite3 and knex-js into my Electron React application

Whenever I try to import sqlite3 to test my database connection, I encounter an error. Upon inspecting the development tools, I came across the following error message: Uncaught ReferenceError: require is not defined at Object.path (external "path ...

Discover how to implement custom data filtering in an Angular controller

Help needed: How can I remove decimals, add $ symbol in an Angular controller? Any ideas? $scope.data = [{ "key": " Logo", "color": "#004400", "values": [ [0, parseInt($scope.myappslogo)] ] }, { "k ...

What is the reason behind utilizing the external 'this' within the inner function in Vue: $this=this?

Recently, I came across some code online that included a line $this=this. My initial interpretation was that this line assigns the 'this' of the outer function to a variable, allowing it to be used in the inner function as well. However, upon fur ...

Trouble Arising from Making a POST Request to Spotify's API

I am currently developing a web application that allows users to search the Spotify Library, add songs to playlists, and then save those playlists to their Spotify Accounts. Almost everything is functioning correctly except for the saving of playlists thro ...

What is causing the issue with using transition(myComponent) in this React 18 application?

Recently, I have been immersed in developing a Single Page Application using the latest version of React 18 and integrating it with The Movie Database (TMDB) API. My current focus is on enhancing user experience by incorporating smooth transitions between ...

The inline style in Angular 2 is not functioning as expected when set dynamically

Having a small dilemma... I'm trying to apply an inline style within a div like this: div style="background: url(..{{config.general.image}})"></div Oddly enough, it was functioning in beta 16 but ever since the RC1 upgrade, it's no longer ...

Error: Angular7 Unable to locate namespace 'google'

I am currently utilizing the import { MapsAPILoader } from '@agm/core'; package to enable auto-complete address search functionality. However, I have encountered an error message stating cannot find namespace 'google'. The error occu ...

Adjust the color of text, image, and background when the cursor hovers over

Is it possible to change the color of an image on hover by applying a filter like brightness(10)? I tried this, but it whitens all the button, so now I'm not sure how to change the icon color. One way to do it is: <div class="hello"><img ...

Selenium highlighting feature is only functional on a particular machine

Having trouble with Selenium code to highlight an element on different systems. Updated both Chrome and Chrome driver, but the highlighting feature only works on one machine. On another machine, the code breaks when attempting to highlight a page element. ...

What is the proper way to change this JavaScript variable into JSON format?

I have a JavaScript variable containing data that I need to convert into valid JSON format for easier parsing. The current variable structure is as follows: { "machines": [{ "category": "Category 1", "items": [{ "name": "Te ...