In this article, we are going to setup the environment and perform the basic test using multiple browsers. For the sake of simplicity we are going to use Firefox and Chrome browser. By default Firefox driver is shipped with the selenium installation. So you don’t need to install any specific Firefox driver for running tests on Firefox browser. However in case of chrome, you’re required to download official chrome webdriver. Let’s start with installation phase.
Installation
You need to install some of the following in order for executing tests with python.
- Python (3.5+ if possible). You can download the most recent version from the official website.
- Chrome WebDriver Installation. Watch the video – Chrome webdriver Setup on Windows.
- Selenium Webdriver.
- Text editor (Try Visual Studio Code or Sublime Text etc).
After you finish installation of Python and Chrome Driver, you need to install Selenium webdriver. We are going to use PIP to install Selenium. Use the following command to install selenium webdriver.
pip install selenium
This will take a minute or two to download all the dependencies and necessary webdriver files. After installation of webdriver package, we are going to write some code and execute our first test with firefox browser. Open code editor and type the following code in it. Save the file with name demo.py or any other name of your choice.
import time
from selenium import webdriverdriver = webdriver.Firefox()
time.sleep(15)
driver.quit()
Code explanation: First line in the code calls for time module. It is required to use the sleep method in our program. We need sleep method to pause the webdriver script for few seconds. After sleep method call, we are going to close the browser using quit().
In second line, we are calling selenium package to import webdriver class.
In third line, we are creating webdriver instance with driver variable.
In fourth line, we are using time.sleep() call to halt the execution for few seconds. You can specify your number in the method.
In fifth line, quite method is being called to close the browser after specific time interval.
Execution:
In order to execute the following code, you need to type the following in the command prompt.
python demo.py
Your filename here could be different. Just replace the name accordingly. When you execute this code you’d be able to use Firefox browser for the test.
Chrome Driver
If you wish to use the Chrome browser for the test then you code needs a bit of modification. Check the code below:
import time
from selenium import webdriverdriver = webdriver.Chrome()
time.sleep(15)
driver.quit()
If you check the third line of the code then you can see that chrome webdriver is called with Chrome() method. Do note that you need to have Chrome webdriver installed in order for this code to work properly.
Now that we have seen how to open a browser, we can move ahead with more specific actions. In case of website testing we’d be needing to do particular actions in order to pass or fail our tests. Here are some of such scenario.
- Open browser
- Go to website.
- Find an element.
- Click on that element.
- Verify the resulted URL.
- Close the browser.
When you create selenium test for above test scenario, you’d be needing to perform various actions using the webdriver. In the below code you can check out how to do just that.
import time
from selenium import webdriverdriver = webdriver.Chrome()
driver.get(‘http://google.com’)
elm = driver.find_element_by_name(‘btnK’)
elm.click()
print(driver.current_url)
time.sleep(15)
driver.quit()
Code explanation: Here we used method find_element_by_name() to point the browser to this location. And also used click() method to click on the element. You can watch the video below to see how you can find and use the Element by ID.
Conclusion
This doesn’t stop here and we have plenty of advanced testing scenarios that we can execute with the Selenium webdriver. In next few tutorials, we’ll tackle some of the advanced testing scenarios. I hope the explanation here helps to understand Selenium WebDriver binding for Python. If you have any questions or feedback, then feel free to share that below comment form.