0

Writing a script using Selenium to open a URL:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.firefox.options import Options as FirefoxOptions
import json

# create a Firefox service and options object
firefox_service = FirefoxService(executable_path='/usr/local/bin/geckodriver')
firefox_options = FirefoxOptions()

# set the binary location for Firefox
firefox_options.binary_location = '/usr/bin/firefox'

# enable network logging
firefox_options.set_preference("devtools.netmonitor.har.enableAutoExportToFile", True)
firefox_options.set_preference("devtools.netmonitor.har.autoConnect", True)
firefox_options.set_preference("devtools.netmonitor.har.forceExport", True)
firefox_options.set_preference("devtools.netmonitor.har.fileName", "network_logs")

# create a Firefox driver instance with the specified service and options
driver = webdriver.Firefox(service=firefox_service, options=firefox_options)

# navigate to the lambdatest website
driver.get("https://www.lambdatest.com/")

# find the header navigation elements and click on each one
header_nav_elements = driver.find_elements_by_css_selector('.navbar-nav .nav-link')
for element in header_nav_elements:
    element.click()

# wait for 5 seconds
driver.implicitly_wait(5)

# close the browser window and quit the driver
driver.close()
driver.quit()

# read the network logs from the HAR file
with open('network_logs.har', 'r') as f:
    network_logs = json.load(f)

# print the network logs
print(json.dumps(network_logs, indent=2))

but when I run it I get an error:

Traceback (most recent call last):
  File "/mnt/c/Users/HP/Desktop/Newstuff/Lambdatest/main.py", line 21, in <module>
    driver = webdriver.Firefox(service=firefox_service, options=firefox_options)
  File "/home/riley/.local/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 199, in __init__
    super().__init__(command_executor=executor, options=options, keep_alive=True)
  File "/home/riley/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/riley/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/riley/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "/home/riley/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable

I tried to use /usr/bin/firefox to test if I could open Firefox with that command but I got

Command '/usr/bin/firefox' requires the firefox snap to be installed.
Please install it with:

snap install firefox 

and when I try snap install firefox I get

snap install firefox
error: cannot communicate with server: Post "http://localhost/v2/snaps/firefox": dial unix /run/snapd.socket: connect: no such file or directory
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    Which binary does it try to run in the first instance, and where is your proper Firefox actually installed? The subsequent errors are a sidetrack if you actually already have Firefox installed; but we can't know where it's installed or how you installed it, only that apparently you used neither Apt nor Snap to install it. So what _did_ you use, and where is the binary? – tripleee Apr 08 '23 at 08:04
  • 1
    Alternatively, if you haven't installed Firefox, everything about Selenium and Python is tangential here, and your central problem is troubleshooting the `snap` issue ... which isn't actually a programming problem; try our sibling site [ubuntu.se] but search for existing questions before asking a new one. – tripleee Apr 08 '23 at 08:05
  • I installed it with apt after installing it from the mozilla site, when i run which firefox i get the path which is /usr/bin/firefox but i cant run the command even after having snap installed – Chukwuneku Apr 08 '23 at 08:59
  • 1
    So you installed it twice, manually somehow by downloading from Mozilla.org, and once with `sudo apt install firefox`? Sounds like `/usr/bin/firefox` is just a shell script which merely exhorts to install via `snap` instead but if you have a real binary (or at least a functional wrapper) in e.g. `/usr/local/bin/firefox` or perhaps somewhere in `/opt`, you just need for that location to be earlier in your `PATH`. Can you run Firefox from outside Selenium, either from your GUI or from the command line? Which location contains that binary? – tripleee Apr 08 '23 at 10:35
  • 1
    Don't use `which`; try `type -all firefox` – tripleee Apr 08 '23 at 10:35
  • Generally, if you need to use `snap` to install firefox, you've _already_ done something wrong. If you're trying to run firefox under selenium, you want a real, native binary that doesn't try to do any extra sandboxing (as `snap` does); you should be installing it with `apt-get` and removing whatever garbage is telling you to use `snap`. – Charles Duffy May 21 '23 at 22:14

1 Answers1

-1

apt-get update

apt-get upgrade

sudo apt-get install snapd

snap --version

sudo apt-get install -yqq daemonize dbus-user-session fontconfig

sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target

exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME

sudo apt install firefox firefox-locale-fr (for frenchies)

snap install firefox

Coumaron
  • 1
  • 1
  • 1
    Many of the commands here make sense, but some of them don't. Consider adding comments describing under what each step accomplishes and under which circumstances it's appropriate. (You tell folks to run `snap --version`, but you never have any step that describes what should be done based on its output). – Charles Duffy May 21 '23 at 22:13
  • 1
    And if you're telling the OP to use `apt install firefox`, why do they need `snap install firefox`? It should be one or the other, not both. – Charles Duffy May 21 '23 at 22:16
  • 1
    (also, the `exec` in `exec sudo nsenter` means that if the `sudo nsenter` fails, the OP's terminal window closes; why would you want that behavior? -- if the answer is "because the LLM said so", note that we don't allow AI-generated answers here) – Charles Duffy May 21 '23 at 22:17