How to run a program silenium with firejail? I want to run Firefox with silenium inside python program.
Example, python file start.py
``
import selenium.webdriver as webdriver
driver = webdriver.Firefox("firejail /usr/bin/geckodriver")
``
Output: FileNotFoundError: [Errno 2] No such file or directory: 'firejail /usr/bin/geckodriver'
But I have geckodriver in /usr/bin folder.
But I have geckodriver in /usr/bin folder.
The error says that it can not find "firejail /usr/bin/geckodriver".
Ok,
driver = webdriver.Firefox("firejail --profile=firefox /usr/bin/geckodriver")
Output FileNotFoundError: [Errno 2] No such file or directory: 'firejail --profile=firefox /usr/bin/geckodriver'
I have Firefox Profile and you can see that I add folder /usr/bin folder. Also I added a folder /tmp (this folder is needed to selenium)
# Firejail profile for firefox
# This file is overwritten after every install/update
# Persistent local customizations
include /etc/firejail/firefox.local
# Persistent global definitions
include /etc/firejail/globals.local
noblacklist ${HOME}/.cache/mozilla
noblacklist ${HOME}/.config/okularpartrc
noblacklist ${HOME}/.config/okularrc
noblacklist ${HOME}/.config/qpdfview
noblacklist ${HOME}/.kde/share/apps/kget
noblacklist ${HOME}/.kde/share/apps/okular
noblacklist ${HOME}/.kde/share/config/kgetrc
noblacklist ${HOME}/.kde/share/config/okularpartrc
noblacklist ${HOME}/.kde/share/config/okularrc
noblacklist ${HOME}/.kde4/share/apps/kget
noblacklist ${HOME}/.kde4/share/apps/okular
noblacklist ${HOME}/.kde4/share/config/kgetrc
noblacklist ${HOME}/.kde4/share/config/okularpartrc
noblacklist ${HOME}/.kde4/share/config/okularrc
# noblacklist ${HOME}/.local/share/gnome-shell/extensions
noblacklist ${HOME}/.local/share/okular
noblacklist ${HOME}/.local/share/qpdfview
noblacklist ${HOME}/.mozilla
noblacklist ${HOME}/.pki
noblacklist /tmp
noblacklist /usr/bin/
include /etc/firejail/disable-common.inc
include /etc/firejail/disable-devel.inc
include /etc/firejail/disable-programs.inc
mkdir ${HOME}/.cache/mozilla/firefox
mkdir ${HOME}/.mozilla
mkdir ${HOME}/.pki
whitelist ${DOWNLOADS}
whitelist /tmp
whitelist /usr/bin/
whitelist ${HOME}/.cache/gnome-mplayer/plugin
whitelist ${HOME}/.cache/mozilla/firefox
whitelist ${HOME}/.config/gnome-mplayer
whitelist ${HOME}/.config/okularpartrc
whitelist ${HOME}/.config/okularrc
whitelist ${HOME}/.config/pipelight-silverlight5.1
whitelist ${HOME}/.config/pipelight-widevine
whitelist ${HOME}/.config/qpdfview
whitelist ${HOME}/.kde/share/apps/kget
whitelist ${HOME}/.kde/share/apps/okular
whitelist ${HOME}/.kde/share/config/kgetrc
whitelist ${HOME}/.kde/share/config/okularpartrc
whitelist ${HOME}/.kde/share/config/okularrc
whitelist ${HOME}/.kde4/share/apps/kget
whitelist ${HOME}/.kde4/share/apps/okular
whitelist ${HOME}/.kde4/share/config/kgetrc
whitelist ${HOME}/.kde4/share/config/okularpartrc
whitelist ${HOME}/.kde4/share/config/okularrc
whitelist ${HOME}/.keysnail.js
whitelist ${HOME}/.lastpass
whitelist ${HOME}/.local/share/gnome-shell/extensions
whitelist ${HOME}/.local/share/okular
whitelist ${HOME}/.local/share/qpdfview
whitelist ${HOME}/.mozilla
whitelist ${HOME}/.pentadactyl
whitelist ${HOME}/.pentadactylrc
whitelist ${HOME}/.pki
whitelist ${HOME}/.vimperator
whitelist ${HOME}/.vimperatorrc
whitelist ${HOME}/.wine-pipelight
whitelist ${HOME}/.wine-pipelight64
whitelist ${HOME}/.zotero
whitelist ${HOME}/dwhelper
include /etc/firejail/whitelist-common.inc
include /etc/firejail/whitelist-var-common.inc
caps.drop all
# machine-id breaks pulse audio; it should work fine in setups where sound is not required
#machine-id
netfilter
nodvd
nogroups
nonewprivs
noroot
notv
protocol unix,inet,inet6,netlink
seccomp
shell none
tracelog
disable-mnt
# firefox requires a shell to launch on Arch.
# private-bin firefox,which,sh,dbus-launch,dbus-send,env,bash
private-dev
# private-etc below works fine on most distributions. There are some problems on CentOS.
# private-etc iceweasel,ca-certificates,ssl,machine-id,dconf,selinux,passwd,group,hostname,hosts,localtime,nsswitch.conf,resolv.conf,xdg,gtk-2.0,gtk-3.0,X11,pango,fonts,firefox,mime.types,mailcap,asound.conf,pulse
private-tmp
noexec ${HOME}
noexec /tmp
EDIT by @rusty-snake: code-block for profile.
Referring to https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver the first argument is the firefox profile.
And /usr/bin is never blacklisted in the profile provided by firejail.
Because of you firefox.profile I assume that you have an firejail-version older than 0.9.60, there you can't use firejail --profile=firefox.
driver = webdriver.Firefox("firejail --profile=firefox /usr/bin/geckodriver")
Output FileNotFoundError: [Errno 2] No such file or directory: 'firejail --profile=firefox /usr/bin/geckodriver'
This is not a shell-command! It tries to open/create the file/directory firejail --profile=firefox /usr/bin/geckodriver and not to execute this.
Maybe you need something like this:
firefox_binary = FirefoxBinary("firejail")
firefox_binary.add_command_line_options("--profile=/etc/firejail/firefox.profile")
firefox_binary.add_command_line_options("/usr/bin/geckodrive")
driver = Firefox(firefox_binary=firefox_binary)
https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.firefox_binary
https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver
start_browser.py file
``
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver import Firefox
firefox_binary = FirefoxBinary("firejail")
firefox_binary.add_command_line_options("--profile=/etc/firejail/firefox.profile")
firefox_binary.add_command_line_options("/usr/bin/geckodrive")
driver = webdriver.Firefox(firefox_binary=firefox_binary)
``
Traceback (most recent call last):
File "/home/mn/AKernel/Program/start_browser.py", line 32, in
driver = webdriver.Firefox(firefox_binary=firefox_binary)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
Process returned 1 (0x1) execution time : 1.343 s
geckodriver.log: Error: invalid -version command line option
This is answer from Henrik Skupin
Note that firejail needs an argument when being called (firejail firefox), and I don't see how this would work with your code above, which doesn't adds that. What you would need is a wrapper script for Firefox which includes that command, and can be called via geckodriver.
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
I think there might be some confusion here around the term capabilities. The above error refers to WebDriver capabilities (see here) and not to firejail's (Linux kernel) capabilities.
@ddd7et still need help?
Yes and no. Yes, because I did not solve problem. No, because I use silenium (firefox) without firejail, and this is normal. I can live without firejail. I use firefox with silenium as the main browser for myself, and I will do this until August 2020. In August 2020, I want to return to this topic and I will try to think about it again.
@ddd7et Could be unrelated to firejail here or this.
This might help you tracking exactly what fails.I would suggest you to step-wise comment in all capability drops (in the loaded profile file) to obtain the thing that does not work.
Does the program work without firejail?
Yes, the program works without firejail. Firefox with selenium webdriver is my main browser. I'm writing this post with firefox selenium without firejail.
@ddd7et Can you post your firefox selenium python script here please. I happen to use geckodriver/curl to open URL's intercepted from other sandboxed applications in an _already running, fully firejailed_ Firefox. IMHO that's easier than starting Firefox via the WebDriver protocol and have it firejailed at the same time. That being said, if I look at code examples in this thread, I don't see any webdriver capabilities being passed. What about something like:
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver import Firefox
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_binary = FirefoxBinary("firejail")
firefox_binary.add_command_line_options("--profile=/etc/firejail/firefox.profile")
firefox_binary.add_command_line_options("/usr/bin/geckodriver")
driver = webdriver.Firefox(firefox_binary=firefox_binary,
desired_capabilities=DesiredCapabilities.FIREFOX)
But without showing us your script this is all just guess-work...
Thank your for your interest to this topic.
This is my script (You can see how I run my Firefox but my full program is much harder, it's a very simple example). So this is file Start_Firefox.py
from selenium.webdriver import Firefox, FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import os
import json
import time
class Browser:
def __init__(self):
profile = FirefoxProfile("/home/mn/.mozilla/firefox/nkm3c5cp.default-release")
binary = FirefoxBinary('/home/mn/Downloads/Firefox2/Firefox/firefox/firefox')
self.driver = Firefox(profile)#, firefox_binary=binary)
self.url = self.driver.command_executor._url
self.session_id = self.driver.session_id
def get_driver(self):
return self.driver
def save_session(self):
conf = {}
conf["url"] = self.url
conf["session_id"] = self.session_id
mfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), "conf_Firefox.json")
with open(mfile, "w") as jsonFile:
json.dump(conf, jsonFile, ensure_ascii=False,
default=str, indent=4, sort_keys=True)
driver = Browser()
driver.save_session()
browser = driver.get_driver()
while True:
try:
browser.switch_to.window(browser.window_handles[-1])
except Exception:
quit()
time.sleep(300) // you can use not 300 but 3
And this works and I can run my Firefox without Firejail. I use this script to run my Firefox silenium as my main (default) browser. I don't use ordinary firefox without silenium.
My script (How I run this py file) (file run.sh)
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
cd $DIR
/usr/bin/python3 $DIR/Start_Firefox.py
If I run your script:
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
firefox_binary = FirefoxBinary("firejail")
firefox_binary.add_command_line_options("--profile=/etc/firejail/firefox.profile")
firefox_binary.add_command_line_options("/usr/bin/geckodriver")
driver = webdriver.Firefox(firefox_binary=firefox_binary,
desired_capabilities=DesiredCapabilities.FIREFOX)
I have the error:
Traceback (most recent call last):
File "/home/mn/Program//test.py", line 9, in
desired_capabilities=DesiredCapabilities.FIREFOX)
File "/home/mn/.virtualenvs/cat/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/home/mn/.virtualenvs/cat/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/mn/.virtualenvs/cat/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/mn/.virtualenvs/cat/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/mn/.virtualenvs/cat/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
...
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
Hm, that makes sense now I look at it again:
...
firefox_binary = FirefoxBinary("firejail")
...
The selenium webdriver tries to obtain the needed capabilities from the firejail binary, which will never succeed obviously. My knowledge of selenium is very limited, as I said, I only use geckodriver to connect to a running Firefox instance- which is already firejailed - and open a URL. That's quite easy in comparison. I'll try putting together a wrapper script and report back here, but to be honest I don't see how to pass this capabilities hurdle.
I'm closing here due to inactivity, please fell free to reopen if you have more questions.