Selenium on Elementary OS not working with Firefox - python

I've got a problem with Selenium on my system. For some reason, it wont launch a Firefox browser window.
Here are the steps that I have gone though.
Downloaded Selenium via pip
Downloaded the Marionette (gecko) driver
Added the directory of the downloaded file to my PATH.
I am still receiving the below error though.
/usr/bin/python2.7 /home/keva161/PycharmProjects/selenium_test.py
Traceback (most recent call last):
File "/home/keva161/PycharmProjects/selenium_test.py", line 21, in <module>
driver = webdriver.Firefox(capabilities=caps)
File "/home/keva161/.local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
self.service.start()
File "/home/keva161/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x7f9bcde911d0>> ignored
The script the I am trying to run is:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
# Tell the Python bindings to use Marionette.
# This will not be necessary in the future,
# when Selenium will auto-detect what remote end
# it is talking to.
caps["marionette"] = True
# Path to Firefox DevEdition or Nightly.
# Firefox 47 (stable) is currently not supported,
# and may give you a suboptimal experience.
#
# On Mac OS you must point to the binary executable
# inside the application package, such as
# /Applications/FirefoxNightly.app/Contents/MacOS/firefox-bin
caps["binary"] = "/usr/bin/firefox"
driver = webdriver.Firefox(capabilities=caps)
driver = webdriver.Firefox()
driver.get('http://saucelabs.com/test/guinea-pig')
driver.quit()
I am using the latest version of Firefox.

PyCharm ignores your PYTHONPATH, instead it builds it based on your project configuration(s), so you need to teach it where it can find gecko. You can do that in either of these 2 ways:
configure your interpreter path to include the gecko's dir, see Interpreter paths
add the gecko's dir as content or source root (see Content Root) and select the respective check box (Add content roots to PYTHONPATH or Add source roots to PYTHONPATH) in the project's run configuration, see Run/Debug Configuration: Python.

Related

WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome browser (1)

I've upgraded Ubuntu to 20.04. It seems that Chrome isn't available as APT package but via snap. After the upgrade I'm getting the error while trying to instantiate Chrome browser:
>>> from selenium.webdriver import Chrome
>>> from selenium.webdriver.chrome.options import Options
>>> o = Options()
>>> o.headless = True
>>> o.add_argument('--disable-dev-shm-usage')
>>> o.add_argument('--no-sandbox')
>>> Chrome(options=o)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/var/www/order/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
chromedriver version is 87.0.4280.20
chromium-browser version is Chromium 87.0.4280.66 snap
I read this discussion. The case is however isn't same. I run python as a regular user. However I've disabled dev-shm-usage and sandbox. But still it doesn't work. It worked before I've upgraded to Ubuntu 20.04. So I assume it has something to do with snap version of Chrome.
I have found out following configuration works:
Start first Chrome chromium-browser --headless --remote-debugging-port=9222 and then:
>>> from selenium.webdriver import Chrome
>>> from selenium.webdriver.chrome.options import Options
>>> o = Options()
>>> o.add_experimental_option('debuggerAddress', 'localhost:9222')
>>> b = Chrome(options=o)
>>> b.get('https://google.com')
>>> b.title
'Google'
>>>
So it seems the problem is with starting the browser.
Opened a bug report
Good explanation was provided by ChromeDriver development team in a response to the ticket I've submitted:
ChromeDriver uses the /tmp directory to communicate with Chromium, but
Snap remaps /tmp directory to a different location (specifically, to
/tmp/snap.chomium/tmp). This causes errors because ChromeDriver can't
find files created by Chromium. ChromeDriver is designed and tested
with Google Chrome, and it may have compatibility issues with
third-party distributions.
There are a couple of workarounds:
Tell ChromeDriver to use a location in your home directory, instead of /tmp. For example, if your home directory is /home/me, you can add
the following line of code to your script:
o.add_argument('--user-data-dir=/home/me/foo')
Explicitly select a port for ChromeDriver to communiate with Chromium. You need to carefully select a port (e.g., 9222) that isn't
already in use, and then add the following line to your script:
o.add_argument('--remote-debugging-port=9222')
and
Another (probably better) workaround is to use the ChromeDriver
installed by Snap, which is at /snap/bin/chromium.chromedriver. E.g.,
driver = Chrome('/snap/bin/chromium.chromedriver', options=o)
Since this version of ChromeDriver runs inside Snap, its /tmp
directory is redirected in the same way as Chromium
The solution to my problem is rather workaround but nevertheless it might be useful.
I have downgraded to Chrome version 86, which I have installed from deb package instead of snap. Installation instructions can be found here
Once I've downgraded chromium and chromedriver to version 86 the problem has gone.
The answer provided by Ralfeus is wholesome, I am using Ubuntu 20.04 and was facing same issues.
I used driver = Chrome('/snap/bin/chromium.chromedriver') , yeah without any additional options and it worked.

geckodriver executable needs to be in path

I have read previous questions asked on this topic and tried to follow the suggestions but I continue to get errors. On terminal, I ran
export PATH=$PATH:/Users/Conger/Documents/geckodriver-0.8.0-OSX
I also tried
export PATH=$PATH:/Users/Conger/Documents/geckodriver
When I run the following Python code
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/Users/Conger/Documents/Firefox.app'
driver = webdriver.Firefox(capabilities=firefox_capabilities)
I still get the following error
Python - testwebscrap.py:8
Traceback (most recent call last):
File "/Users/Conger/Documents/Python/Crash_Course/testwebscrap.py", line 11, in <module>
driver = webdriver.Firefox(capabilities=firefox_capabilities)
File "/Users/Conger/miniconda2/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
self.service.start()
File "/Users/Conger/miniconda2/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x1006df6d0>> ignored
[Finished in 0.194s]
you may downgrade your selenium by
pip install selenium==2.53.6
This has solved my issue
On mac:
brew install geckodriver
Homebrew is the most popular package manager for Mac OS X, you will need install XCode on your mac and it will be then accesible from your terminal.
You can follow this tutorial if required
First we know that gekodriver is the driver engine of Firefox,and we know that
driver.Firefox() is used to open Firefox browser, and it will call the gekodriver engine ,so we need to give the gekodirver a executable permission.
so we download the latest gekodriver uncompress the tar packge ,and put gekodriver at the /usr/bin/
ok,that's my answer and i have tested.
I just downloaded the latest version geckodriver (I have win7) from here and added that exe-file in python directory (which already in PATH)
export path works only in the terminal you have entered the command. If you try to run the script from a different terminal, you will get the same error.

Firefox driver can't start for Selenium 3.0.1 with FF49 and Python

i have following Selenium Webdriver script with Python. But i got error:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps)
driver.get("http://www.mahsumakbas.net")
print driver.title
driver.close()
error is:
Traceback (most recent call last): File
"C:\Mahsum\DevelopmentWorkSpace\Eclipse\Java\selenium_proj\src\hello.py",
line 6, in
driver = webdriver.Firefox(capabilities=caps) File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py",
line 135, in init
self.service.start() File "C:\Python27\lib\site-packages\selenium\webdriver\common\service.py",
line 71, in start
os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'geckodriver'
executable needs to be in PATH.
Exception AttributeError: "'Service' object has no attribute
'process'" in >
ignored
Selenium Webdriver version is: 3.0.1
Firefox: 49.0.2
geckodriver: v0.11.1-win64
i added geckodriver path to Windows PATH variable.
where is the problem?
You can place the 'geckodriver' .exe in the base path of Python and it will work.
Alternatively, you have to declare the path to geckodriver when initializing if you prefer to have a clean Python folder. Either do it every time you run your script or by PATH as you says you've done. As Naveen suggests, a reboot is necessary before a PATH is correctly saved. You could also try to run this in the Windows command line:
setx path "%path%;c:\path\to\geckodriver-folder"
final code is like follow and working:
binary = FirefoxBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe")
driver = webdriver.Firefox(firefox_binary=binary)
set path of geckodriver.exe without file name(only folder that it is placed) to PATH vairable.
this time, i have another problem:
driver.close() doesn't close firefox.
when change as driver.quit() it closes but following line is appear on console:
'NoneType' object has no attribute 'path'
there is not any indicator to show it is warning or error. Just line itself.
Try to add firefox profile
profile = webdriver.FirefoxProfile()
webdriver.Firefox(capabilities=caps,firefox_profile=profile)
from selenium import webdriver
# To Run on FireFox Browser
self.driver = webdriver.Firefox(executable_path="C:/Drivers/geckodriver.exe")
driver.get("http://www.mahsumakbas.net")
print(driver.title)
driver.close()

launch selenium from python on ubuntu

I have the following script
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://localhost:8000')
assert 'Django' in browser.title
I get the following error
$ python3 functional_tests.py
Traceback (most recent call last): File "functional_tests.py", line 3, in <module>
browser = webdriver.Firefox() File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 80, in __init__
self.binary, timeout) File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in __init__
self.binary.launch_browser(self.profile, timeout=timeout) File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
self._wait_until_connectable(timeout=timeout) File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 99, in _wait_until_connectable
"The browser appears to have exited " selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
pip3 list shows selenium (2.53.6).
firefox -v shows Mozilla Firefox 47.0.
I struggled with this problem as well, and I was unhappy with having to use older versions of Firefox. Here's my solution that uses the latest version of Firefox. It however involves several steps
Step 1. Download v0.9.0 Marionette, the next generation of FirefoxDriver, from this location: https://github.com/mozilla/geckodriver/releases/download/v0.9.0/geckodriver-v0.9.0-linux64.tar.gz
Step 2. Extract the file to a desired folder, and rename it to "wires". In my case I created a folder named "add_to_system_path" under Documents. So the file is in Documents/add_to_system_path/wires (also make sure that the wires file is executable under its properties)
Step 3. Create a file named ".pam_environment" under your home folder, and then adding this line on it and save
PATH DEFAULT=${PATH}:/absolute/path/to/the/folder/where/wires/is/saved
What this does is it tells ubuntu to add the enumerated dir in .pam_environment to your system path
Step 4. Save the file, log out of your user session, and log back in. This is necessary to do so that the files in the newly added system path is recognized by ubuntu
Step 5. Use the code below to instantiate the browser instance:
`
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = DesiredCapabilities.FIREFOX
capabilities["marionette"] = True
browser = webdriver.Firefox(capabilities=capabilities)
browser.get('http://your-target-url')`
Firefox should now be able to instantiate as usual.
The last version of Firefox is not working properly with selenium. Try with 46 or 45.
You can download here: ftp.mozilla.org/pub/firefox/releases
or sudo apt-get install firefox=45.0.2+build1-0ubuntu1
You can also do this graphically as shown here http://www.howtogeek.com/117929/how-to-downgrade-packages-on-ubuntu/

WebDriverException: Message: Can't load the profile. At a loss as to what is happening

I've been using webdriver.Firefox() for years now, today I started another project and browser = webdriver.Firefox() is returning
WebDriverException: Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.
I have tried adding C:\Program Files (x86)\Mozilla Firefox\firefox.exe to my PATH. I have tried pip install -U selenium. I've tried
binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
browser = webdriver.Firefox(firefox_binary=binary)
profile = FirefoxProfile("C:\Users\Me\AppData\Roaming\Mozilla\Firefox\Profiles\8u2w2ge1.Me")
browser = webdriver.Firefox(profile)
I have added the addon checkcompatibility to my firefox addons. Nothing, nothing is working, same error, Python 2.7.11, Windows 10 x64
This is the general traceback I am getting.
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
browser = webdriver.Firefox(firefox_binary=binary)
File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 80, in __init__
self.binary, timeout)
File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\extension_connection.py", line 52, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 68, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 108, in _wait_until_connectable
% (self.profile.path))
WebDriverException: Message: Can't load the profile. Profile Dir: c:\users\me\appdata\local\temp\tmpwvgny8 If you specified a log_file in the FirefoxBinary constructor, check it for details.
Anyone know why it is having issues creating this profile in the temp directory? Some new way to force it to profiles that exist, because my old method is not working.
I've actually found a work around for this, it isn't perfect and I actually use chrome through chromedriver, since it is a bit more stable, but to use firefox you might want to download a geckodriver and include it in your system path or path to it in your setup of the browser, https://github.com/mozilla/geckodriver/releases, seems to be where they keep releases, chrome has a chromedriver that functions and is used in much the same way. I am not sure why we need these new drivers to use selenium in windows, but such is life, hopes this saves someone else some work.

Categories