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.