jaibalaji
jaibalaji

Reputation: 3475

How to set up a Selenium Python environment for Firefox?

How can I set up a Selenium Python environment for Firefox?

I am using Firefox 50, Selenium 3, Python 3.5. I tried with many things binary and copying the geckodriver in the environment variable PATH, etc.

Upvotes: 18

Views: 134079

Answers (5)

Since Selenium 4.6.0, you don't need to manually install Selenium Manager(webdriver-manager) as shown below because it is already included in Selenium according to the blog:

pip install webdriver-manager

And, since Selenium 4.11.0, the code below is basically enough because Selenium Manager can automatically discover your browser version installed in your machine, then can automatically download the proper driver version for it according to the blog:

from selenium import webdriver

firefox_driver = webdriver.Firefox()

And, the examples below can test Django Admin with Firefox, Selenium, pytest-django and Django. *My answer explains how to test Django Admin with multiple Headless browsers(Chrome, Microsoft Edge and Firefox), Selenium, pytest-django and Django:

# "tests/test_1.py"

import pytest
from selenium import webdriver
from django.test import LiveServerTestCase

@pytest.fixture(scope="class")
def firefox_driver_init(request):
    firefox_driver = webdriver.Firefox()
    request.cls.driver = firefox_driver
    yield
    firefox_driver.close()

@pytest.mark.usefixtures("firefox_driver_init")
class Test_URL_Firefox(LiveServerTestCase):
    def test_open_url(self):
        self.driver.get(("%s%s" % (self.live_server_url, "/admin/")))
        assert "Log in | Django site admin" in self.driver.title

Or:

# "tests/conftest.py"

import pytest
from selenium import webdriver

@pytest.fixture(scope="class")
def firefox_driver_init(request):
    firefox_driver = webdriver.Firefox()
    request.cls.driver = firefox_driver
    yield
    firefox_driver.close()
# "tests/test_1.py"

import pytest
from django.test import LiveServerTestCase

@pytest.mark.usefixtures("firefox_driver_init")
class Test_URL_Firefox(LiveServerTestCase):
    def test_open_url(self):
        self.driver.get(("%s%s" % (self.live_server_url, "/admin/")))
        assert "Log in | Django site admin" in self.driver.title

Upvotes: 2

Beliaev Maksim
Beliaev Maksim

Reputation: 1579

If you are working on Ubuntu and the latest firefox. You may find some issues since firefox is bundled now in snap.

To resolve the issue with driver not being able to connect to the firefox, you will need to use driver bundled with snap.

I bundled an ready to go example here: https://github.com/beliaev-maksim/firefox-selenium

However, for quick access here is the content of conftest.py for quickly setup a driver fixture

import pytest
from selenium import webdriver

from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service


@pytest.fixture(scope='session')
def driver(request):
    """Set up webdriver fixture."""
    options = Options()
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')

    service = Service(executable_path="firefox.geckodriver")
    driver = webdriver.Firefox(options=options, service=service)
    driver.set_window_size(1920, 1080)
    driver.maximize_window()
    driver.implicitly_wait(10)

    yield driver
    driver.quit()

Upvotes: 2

IslamTaha
IslamTaha

Reputation: 1564

The testing machine should have Selenium v. 3.0.2, Firefox v. 51.0.1 (latest version) and geckodriver v. 0.14. If you are using Linux, please do the following steps:

[Look up the latest release on GitHub (or from the API) and replace the wget link with that. Downloading and installing an outdating release may result in "buggy" behaviour.]

apt-get update
apt-get install firefox
pip3 install selenium==3.0.2
wget https://github.com/mozilla/geckodriver/releases/download/vX.XX.0/geckodriver-vX.XX.0-linuxXX.tar.gz -O /tmp/geckodriver.tar.gz \
  && tar -C /opt -xzf /tmp/geckodriver.tar.gz \
  && chmod 755 /opt/geckodriver \
  && ln -fs /opt/geckodriver /usr/bin/geckodriver \
  && ln -fs /opt/geckodriver /usr/local/bin/geckodriver

Select the version for your operating system from the available compressed pre-built binaries.

Here is an example to run:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://google.com')
print(driver.title)
driver.quit()

Upvotes: 27

jaibalaji
jaibalaji

Reputation: 3475

  1. In Windows install Python from: https://www.python.org/downloads/

  2. Then run pip install from the command line: pip install selenium

  3. Download the Gecko/Chrome/Internet Explorer driver and add the driver.exe path to the PATH environment variable. So the need to set up the path while running Selenium driver.Firefox() / driver.Chrome() method.

Upvotes: 2

torina
torina

Reputation: 4433

As far as I understand, you want to develop in Python, using the Selenium library and work with the Firefox webdriver.

  1. Install Python (Python 3 already contains pip)
  2. Install Selenium (pip install selenium or some IDEs like PyCharm propose to install libraries, just import Selenium)
  3. Download Mozilla webdriver
  4. Enjoy!

Upvotes: 12

Related Questions