First Example on how to use Selenium with Python

Selenium.webdriver module provides all the WebDriver implementations. WebDriver offers a multiple ways to find element/ find elements using one of the find_element_by_* methods.

Webdriver offers more than one way to locate the elements of your web application in order to find the elements. You can find elements by using ID, name, XPath, CSS Selectors, and more. Know more on Locators for Selenium

Please save the below script (eg:- python_selenium_example.py), and then it can be run like below:

python python_selenium_example.py

Let us look into the below example :-

Step 1:- Open Chrome browser

Step 2:- Assert Title

Step 3:- Enter Test Python text

Step 4:- Assert text entered

Step 5:- Close browser

from selenium import webdriver

driver = webdriver.Chrome(r'C:\Users\drivers\chromedriver.exe')
driver.maximize_window()
driver.get("http://www.seleniumeasy.com/test/basic-first-form-demo.html")
assert "Selenium Easy Demo - Simple Form to Automate using Selenium" in driver.title

eleUserMessage = driver.find_element_by_id("user-message")
eleUserMessage.clear()
eleUserMessage.send_keys("Test Python")

eleShowMsgBtn=driver.find_element_by_css_selector('#get-input > .btn')
eleShowMsgBtn.click()

eleYourMsg=driver.find_element_by_id("display")
assert "Test Python" in eleYourMsg.text
driver.close()

Now, let's understand the above code in detail

We are opening Google chrome browser and using "driver.maximize_window" method maximize the browser window.

driver = webdriver.Chrome(r'C:\Users\pc\Downloads\chromedriver.exe')
driver.maximize_window()

"driver.get()" method will open the web page by using the URL we provide.

driver.get("http://www.seleniumeasy.com/test/basic-first-form-demo.html")

Finding input text field by id, and clearing it before entering any text using "clear" method. And then entering data in it by using "send_keys()" method.

eleUserMessage = driver.find_element_by_id("user-message")
eleUserMessage.clear()
eleUserMessage.send_keys("Test Python")

Finding "Show Your Message" button element by css selector. In CSS selectors id is defined using "# and class is defined using ".". Earlier we have discussed multiple example using css selectors.

Then clicking on the button using "click()" method.

eleShowMsgBtn=driver.find_element_by_css_selector('#get-input > .btn')
eleShowMsgBtn.click()

Assert whether the input text and output text are same using assertion.

eleYourMsg=driver.find_element_by_id("display")
assert "Test Python" in eleYourMsg.text

"driver.close()" method is used for closing the web browser after performing the tests.

driver.close()

In the next section of the tutorial, we will execute the above example using unittest which is a unit testing framework was originally inspired by JUnit.

Selenium Tutorials: 

Comments

The links for the test pages are broken and result in page not found.
For example this page has this code:
driver.get("http://www.seleniumeasy.com/test/basic-first-form-demo.html")
But that page does not exist. A search for the page gives no results

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.