If you are interested in automated testing web applications with Selenium or Webdriver but you prefer python over Java this getting started guide can help you with basic steps and setup.
The first step is to install python. You can download it from the official site (https://www.python.org/downloads/) or if you use Linux install it from the repositories.
For Debian / Ubuntu users:
$ sudo apt-get install python
For RedHat:
$ sudo yum install python
Or
# yum install python
If you are not sure about which version of python is installed or if you have any of them on your machine just check it out on console / terminal typing the following command:
python –-version
The next step is to install Webdriver for python. The easiest way is using pip for installation (python package management system). In Python 3.4 pip is included by default but for older versions you have to install it manually (<a href=”https://pip.pypa.io/en/latest/installing.html#install-pip” target=”new” data-mce-href=”https://pip.pypa.io/en/latest/installing.html#install-pip”>https://pip.pypa.io/en/latest/installing.html#install-pip</a>). After that the installation of Selenium looks like:
pip install selenium
You are ready to create your test case. Here is an example which presents the basics.
from selenium import webdriver from selenium.webdriver.common.by imp ort By from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.ui import WebDriverWait def waitForElementToAppear(by, locator): return wait.until(expected_conditions.visibility_of_element_located((by, locator))) except TimeoutError: print("Element is not appeared") driver = webdriver.Firefox() driver.get("http://www.google.com") if not "Google" in driver.title: raise Exception("Unable to load Google page!") webelement = driver.find_element_by_name("q") webelement.send_keys("wedoqa") webelement.submit() topResult = waitForElementToAppear(By.CSS_SELECTOR, "ol>li.g .rc h3 a") if not "wedoqa" in driver.title: raise Exception("Result page is not loaded") print("The top result is: " + topResult.text) driver.quit()
To run the test above create a file with .py extension (for instance test.py) and copy the code into the file. If this is finished open a console or terminal and navigate to the folder containing the file which was created before. Run the test with the following command:
python yourFile.py
The expected result is: “The top result is: WeDoQA”
This test is compatible with python 3 so if you experience some trouble with running it check your python version.
Documentation and useful information is available on the official selenium – python site: https://selenium-python.readthedocs.org/
Similar Posts from the author:
- Getting started with Android automation testing
- Page Object pattern and Page Factory
- Event logging with aspects in automated testing
- Graphwalker – how to start guide
- Overview of automated testing
2 thoughts to “Getting started with Webdriver python guide”
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class SearchTests(unittest.TestCase):
def setup(self):
# create a new Firefox session
self.driver = webdriver.Chrome(“C:\Users\Dreamthought\Downloads\chromedriver_win32”)
self.driver.implicitly_wait(30)
self.driver.maximize_window()
I am getting a bunch of error message while running the above code. Please help me to fix the issue.
C:UsersDreamthoughtAppDataLocalProgramsPythonPython35-32python.exe “C:Program Files (x86)JetBrainsPyCharm Community Edition 2016.2.1helperspycharmutrunner.py” C:UsersDreamthoughtPycharmProjectsMyfirstseleniumMyPythonProgramBasicscript.py true
Testing started at 7:56 AM …
Traceback (most recent call last):
File “C:Program Files (x86)JetBrainsPyCharm Community Edition 2016.2.1helperspycharmutrunner.py”, line 153, in
modules = [loadSource(a[0])]
File “C:Program Files (x86)JetBrainsPyCharm Community Edition 2016.2.1helperspycharmutrunner.py”, line 65, in loadSource
module = imp.load_source(moduleName, fileName)
File “C:UsersDreamthoughtAppDataLocalProgramsPythonPython35-32libimp.py”, line 172, in load_source
module = _load(spec)
File “”, line 693, in _load
File “”, line 673, in _load_unlocked
File “”, line 661, in exec_module
File “”, line 767, in get_code
File “”, line 727, in source_to_code
File “”, line 222, in _call_with_frames_removed
File “C:UsersDreamthoughtPycharmProjectsMyfirstseleniumMyPythonProgramBasicscript.py”, line 7
def setup(self):
^
IndentationError: expected an indented block
Process finished with exit code 1
Hello Lucky,
As the error message says you are having problems with indentation in line 7 of you code. Make sure that you have the correct indentation(tab or 4 spaces) after def setup(self):
Comments are closed.