App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

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

How to install Selenium Python on macOS?

By Tom Collins, Community Contributor -

The Integrated Development Environment (IDE) of Selenium boosts the automation script’s speed and provides reusability. As a result, you can use one script on different browsers for automation testing. Also, it’s easy to learn for engineers with less coding knowledge. The Selenium scripts support any language – Python, Java, C#, etc.

  • Moreover, the Selenium Grid supports the parallel execution of test cases on different browsers and platforms simultaneously.
  • Additionally, this grid decreases the test execution time.
  • Also, it is easy to integrate Selenium with Maven, Jenkins, and Docker testing tools and connect with test management tools like TestNG and Junit.

Python is an open-source scripting language that is easy to learn and more understandable than other programming languages. It provides a default framework named PyUnit or Unittest for automation testing. Additionally, it provides built-in testing frameworks like Pytest and Robot. Also, it is easily embedded with other programming languages such as C, C++, JAVA, etc.

What is Selenium Python?

Selenium Python is a bond or crucial resource of APIs that helps write test scripts using Selenium WebDriver. Selenium Python APIs cover competent tests to interact with web elements. 

This binding is available for different web browsers and provides a remote WebDriver that allows connecting with remote cloud Selenium Grid.

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 

(for Python 3)

python3 –version 

Then we 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 

(for Python3)

Python3 get-pip.py

To verify the installation, run the command as:

(for Python2)

Pip –version

(for Python3)

Pip3 --version

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

pip install selenium

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

This way, our machine is ready with Selenium Python.

How to use Selenium with Python?

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

It stores all the data about how we interact with the UI (user interface) or application. Thus we 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 let’s solve a simple problem regarding this topic. Consider the following BrowserStack’s config file as our test case and execute the test step by step.

For Example: We will 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"

}

]

}
  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()

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()

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

   Python selenium_test.py

Final Output:

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

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()

Closing Notes

Selenium Python binding becomes helpful during UI testing in automation. Most preferably, it is helpful during short development lifecycles, and 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. We can integrate Selenium WebDriver with this framework, also.

BrowserStack Automate offers 3000+ 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

Featured Articles

Page Object Model and Page Factory in Selenium Python

How to perform Web Scraping using Selenium and Python

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack