Easy MacOS setup of Selenium and Geckodriver
Selenium can be fun to play with, but really best used for website testing. I needed to see if I could quickly setup and test how Selenium works, so that I can prepare some tests for the web client of the software package I work with at the office. And I was really impressed on how quick and easy it was to setup and test.
Virtual Environment
For any project I do, I setup a Python Virtual Environment, and you can check out my quick article on setting this up. While it is not required, it is a very good idea to use a virtual environment to play with Selenium so that you don’t install versions of software that may affect your other projects.
Install Geckodriver
You will need a webdriver for the web browser you wish to control. There is an unofficial Selenium documentation that talks about the different drivers. I chose to go with the Firefox Geckodriver for this setup. You will need to look up how to install the Geckodriver for your platform, but for MacOS this was very simple by using HomeBrew. I will only describe how I setup my Mac here. From the Terminal window type the following:
brew install geckodriver
This will download and install the Geckodriver in your PATH, so that Selenium and Python can find the driver. Otherwise, if you choose to download the driver into another folder you will need to tell the webdriver where to find the file inside your Python script
Install Selenium
You will also need to install the Selenium Python third party library. Be sure that you have started your virtual environment, and then from the Terminal window type the following:
pip install selenium
Once the package is downloaded, you are now ready to setup a script and start testing
A simple script to test
We are going to use a very simple script to open Firefox to the Python.org website, and then enter text into the search box and submit the search and view the results. I got this script from the unofficial Selenium documentation for getting started, but added a timer to allow an opportunity to at least see the results before python shuts down the browser. Create a file called “pyconsearch.py” (or whatever name you like) and paste in the following code:
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
sleep(30)
driver.close()
You can then save and run the file from a Terminal window from the directory you saved this file by typing:
python pyconsearch.py
Script explanation
We first add our imports:
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
I added the sleep module, so that after we get our results I can put the script to sleep for a short while to give us opportunity to view the results before the webdriver closes the tab or browser
We then add the webdriver module for selenium to control our browser, and the Keys module so we can send keystrokes to the browser.
Next get the page
Now to get a page loaded into a browser
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
First initialize the webdriver to open Firefox. The selenium webdriver will look for the correct driver based on the brower function you call. In this case, I am calling for the webdriver to load up Firefox. The webdriver will look in our system PATH directories to find the geckodriver we installed earlier. We then tell the driver to ‘get
‘ the website page for python.org and run an assert test to make sure that the correct page was loaded by checking that the text “Python
” is in the title of the browser tab.
Find the search box on the page
We then want to find the search box and enter some text to submit for a search.
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
First, tell the webdriver to search the HTML of the page to find a form input element that has name='q'
and save into a variable we can use called elem. We will first clear the search box input element just incase there was data typed in before we set the value via the script. (a good practice). We then use the send_keys() function to first mimic a user typing the text “pycon
” into the element, and then mimic a user hitting the “RETURN” key.
Sending the RETURN keystroke should cause the form to activate the SUBMIT button of the form which reloads the page with the search results. We then use an assert to test that the results page does not have text in the page source that says “No results found”. This test should probably be setup to do something if there were no results found, like maybe allow a user of the script to submit a different search, but this is just a simple script to show test case works.
Pause and review results
After we get our results, it is best to have a chance to at least see what they are.
sleep(30)
driver.close()
We use the sleep() function to tell python to wait about 30 seconds, and hopefully that will be enough to allow us to view the results before the webdriver closes the browser tab or window. While this does not allow us much time to do anything with the results, this script at least gives us a small taste of what we can do with selenium and create more involved scripts.
Troubleshooting
I really don’t have an idea as to what other resources to try other than what I have given above if there are any issues, but feel free to leave a comment below and I will do my best to answer, and then add to this portion of the page. I’m sure after I get a few responses, I will hopefully be able to add some tips.