How to install Selenium Python on macOS?

Learn through this step-by-step automation tutorial on installing and implementing Selenium with Python on macOS.

Get Started free
How-to-install-Selenium-Python-on-macOS
Home Guide How to install Selenium Python on macOS?

How to install Selenium Python on macOS?

Selenium Python is a framework for automating web browser interactions, making it essential for testing and web scraping. Setting it up on macOS ensures smooth automation across different browsers.

Overview

Selenium Python is popularly used for automating web interactions on macOS, leveraging Python’s simplicity with Selenium’s powerful browser automation capabilities.

Benefits of Installing Selenium Python on macOS

  • Cross-Browser Compatibility: Automates tests across Chrome, Safari, and Firefox.
  • Seamless Integration: Works with testing frameworks like PyTest and unit tests.
  • Fast and Reliable: Enhances test execution speed and accuracy.
  • Supports Remote Testing: Easily connects to cloud-based Selenium Grid.
  • Easy Setup & Maintenance: Simple installation and updates using PIP.

This article covers Selenium Python, its benefits for automation, installation on macOS, usage, and running tests on BrowserStack.

What is Selenium Python?

Selenium Python is a powerful open-source framework that allows testers and developers to automate web applications using the Python programming language. It provides a simple yet effective interface to interact with web elements, simulate user actions, and perform automated browser testing across different environments.

Selenium Python provides a set of APIs that enable writing test scripts using Selenium WebDriver. These APIs facilitate efficient interaction with web elements, making automated testing more effective.

The Selenium Python bindings support multiple web browsers and include a remote WebDriver, allowing seamless connection to a cloud-based Selenium Grid for distributed testing.

Why is Selenium Python preferred for Automation?

Automation testing is the process by which manual test cases are converted into automated test scripts using automation tools like Selenium. The primary reason for using automation is it is time-saving and faster. It becomes an excellent choice to select this combination for automation cause both of them support cross-browser testing. Thus, Selenium Python is preferred for automation testing.

Installing Selenium and Python on macOS

Python Installation: A benefit of using macOS is that Python is installed by default in it.

To verify whether Python is installed or not, just run the command on terminal:

(for Python 2)

python version 
Copied

(for Python 3)

python3 version 
Copied

Then you need a Package Installer for Python (PIP) to install Selenium Python. It’s the package management system for python. Run the below command to install it:

(for Python2)

Python get-pip.py 
Copied

(for Python3)

Python3 get-pip.py
Copied

To verify the installation, run the command as:

(for Python2)

Pip version
Copied

(for Python3)

Pip3 --version
Copied

Open the terminal to install Selenium in your system, and run command:

pip install selenium
Copied

After doing this, a folder named ‘Selenium’ should be created within the ‘Python’ folder.

Now update the existing version of Selenium. So, run the command:

pip install selenium
Copied

This way, our machine is ready with Selenium Python.

How to use Selenium with Python?

By using a Configuration File, you can implement Selenium with Python. About: ‘Configuration’ means ‘settings’. In simple words, a configuration file keeps the information about the settings and infrastructures of you application. It is also called Config File.

It stores all the data about how you interact with the UI (user interface) or application. Thus you need it.

  • Role of the config file in Selenium Python: As Selenium is used to perform cross-browser testing in automation, the config files save information about different possible browsers and devices within it.
  • Selenium Python in Automation: Selenium is used to test some common tasks in automation. They are – visiting a webpage, page scrolling, pressing keys, clicking on clickable elements, etc.

Now take an example of a simple problem related to this topic. Consider the following BrowserStack’s config file as a test case and execute the test step by step.

For Example: You have a straightforward test case of opening Google and searching for a search_term and pressing enter. This search_term will be stored in a config file browserstack_config_v2.json That file contains the below configuration.

{

"browsers": [

{

"browser": "chrome",

"browser_version": "latest",

"search_term": "browserstack"

},

{

"browser": "firefox",

"browser_version": "latest",

"search_term": "browserstack automate"

}

]

}
Copied
  1. Import required library.
  2. Upload the config file.
  3. Perform the test and check whether the output becomes successful or not.
curr_config = config_data['browsers'][0]

curr_browser = curr_config['browser']

curr_search_term = curr_config['search_term']




if(curr_browser=='chrome'):

driver = webdriver.Chrome('path to your chromedriver.exe')

elif(curr_browser=='firefox'):

driver = webdriver.Firefox(executable_path=''path to your geckodriver.exe')

try: 

print('1. Opening Website')

url = 'http://www.google.com'

driver.get(url)

print('2. Performing test')

inputElement = driver.find_element(By.NAME, 'q')

inputElement.send_keys(curr_search_term)

inputElement.submit()

print('3. Test is Successful')

except Exception as e:

print('Test is Unsuccessful')

driver.close()
Copied

If the test ran successfully, then the output will be shown as below:

  • Open Website
  • Performing test
  • Test is Successful

Now save the python code as a Selenium test file.

from selenium import webdriver 

from selenium.webdriver.firefox.options import Options

from selenium.webdriver.common.by import By

import json

import warnings

warnings.filterwarnings("ignore")




with open('browserstack_config_v2.json') as json_file:

config_data = json.load(json_file)





for curr_config in config_data['browsers']:

curr_browser = curr_config['browser']

curr_search_term = curr_config['search_term']




if(curr_browser=='chrome'):

driver = webdriver.Chrome('path to your chromedriver.exe')

elif(curr_browser=='firefox'):

driver = webdriver.Firefox(executable_path=’path to your geckodriver.exe')




try: 

print('1. Opening Website')

url = 'http://www.google.com'

driver.get(url)

print('2. Performing test')

inputElement = driver.find_element(By.NAME, 'q')

inputElement.send_keys(curr_search_term)

inputElement.submit()

print('3. Test is Successful')

except Exception as e:

print('Test is Unsuccessful')

driver.close()
Copied

Open the Mac terminal. Execute the command to run the test and measure final output.

   Python selenium_test.py
Copied

Final Output:

  • Open Website
  • Performing test of searching: BrowserStack
  • Test is Successful

Talk to an Expert

How to run Selenium Python on BrowserStack?

  • Login to BrowserStack Automate.
  • Set a device-browser combination for running the test.
  • Then run the following sample code on your terminal.
from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.desired_capabilities importDesiredCapabilities




desired_cap = {

'browserName': 'android',

'device': 'Samsung Galaxy Note 9',

'realMobile': 'true',

'os_version': '8.1',

'name': 'Bstack-[Python] Sample Test'

}

 

driver = webdriver.Remote(

command_executor='https://<<user>>:<<user.keys>>@hub.browserstack.com:80/wd/hub',

desired_capabilities=desired_cap)

 

driver.get("https://www.google.com")

if not "Google" in driver.title:

raise Exception("Unable to load google page!")

elem = driver.find_element_by_name("q")

elem.send_keys("BrowserStack")

elem.submit()

print (driver.title)

driver.quit()
Copied

BrowserStack Automate Banner

Conclusion

Selenium Python binding becomes helpful during UI testing in automation. Most preferably, it is helpful during short development lifecycles, where new features have to be added frequently in a month as per the user’s requirements.

  • With the Selenium WebDriver, test cases can be executed on various browsers.
  • The TestNG framework helps to execute one test case on different browsers simultaneously on a single machine. You can integrate Selenium WebDriver with this framework, also.

BrowserStack Automate offers 3500+ real browsers and devices for Selenium Python testing on a Cloud Selenium Grid to improve the cross-browser testing experience by end-users.

Start Selenium Testing

Tags
Automation Testing Selenium