How do I validate to see if a text field has the value that I typed in?

I've very new to Python and Selenium and I think I need to use the Assert command to verify a text field has what I typed in via Selenium.

I've very new to Python and Selenium and I think I need to use the Assert command to verify a text field has what I typed in via Selenium.

I've searched for an hour and can't find the answer.

Here is my code:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains as AC
from import By
from selenium.webdriver.common.keys import Keys
from import expected_conditions as EC
from import Select, WebDriverWait

driver = webdriver.Ie()


WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/form/table/tbody/tr[5]/td/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[1]/td[1]/input"))).send_keys("tess893")

element = driver.find_element_by_xpath("/html/body/div/form/table/tbody/tr[5]/td/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[1]/td[1]/input")
assert element.text == "tess893"

Here are my results:

Traceback (most recent call last):
File "c:\Users\uswarv41.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\", line 45, in <module>
File "c:\Users\uswarv41.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\", line 265, in main
File "c:\Users\uswarv41.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\", line 256, in handle_args
run_main(addr, name, kind, *extra, **kwargs)
File "c:\Users\uswarv41.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\", line 52, in run_main
runner(addr, name, kind == 'module', *extra, **kwargs)
File "c:\Users\uswarv41.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\", line 32, in run
File "c:\Users\uswarv41.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd_vendored\pydevd\", line 1283, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "c:\Users\uswarv41.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd_vendored\pydevd\", line 1290, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "c:\Users\uswarv41.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd_vendored\", line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "c:_TMP\", line 18, in <module>
assert element.text == "tess893"

I don't know why am I getting an AssertionError. Based on what I read, it should not throw any errors.

Selenium with Python

Selenium with Python

Selenium with Python

Learn end to end selenium with python. start with learning python as a programming language and then progress to learn selenium as a test automation tool.

Basics of python
Data types
if, while, for
file handling, regex
Basics of selenium
Selenium ide
Parameterization and Synchronization
Handling different web elements
Action class
Page object model
Excel handling
Bdd behave
All these are covered.

Who is the target audience?

Manual Testers, Developers, Team Leads, Test Managers, Anyone interested in learning Selenium with Python
Basic knowledge
Basics of manual testing
What will you learn
Automate web application end to end using selenium with python as a programming language

Selenium WebDriver With Python 3.x - Novice To Ninja

Selenium WebDriver With Python 3.x - Novice To Ninja

Selenium WebDriver With Python 3.x - Novice To Ninja

Best Selenium WebDriver With Python Programming Language Course - Complete Framework Implementation From Scratch

This course includes a wide range of topics from Selenium WebDriver Basics and Advanced, Python Programming Concepts, Unittest And Pytest Framework, Automation Framework Design (Page Objects, Data Driven, Reading CSV Files), Logging Infrastructure, Cross-Browser Testing, Interview Preparation. All materials including code files.

You achieve two targets with one single course

Complete Python Programming Language
Selenium WebDriver Automation
Why Python Programming Language?

Python Programming Language is the fastest growing programming language within both the fields, development and testing. QA industry is rapidly progressing to move on with Python Programming Language for all the automation needs because of the flexibility it offers.

One stop place to become an Expert in Web Automation Framework related technologies from scratch

We start from Beginners level and go through Advance level. This is a single course for everything you need to know related to Web UI Automation.

To read more:

Selenium Python

Selenium Python

Selenium Python - is one of the widely used tools used for Web Browser Automation, and offers a lot of functionality and power over a browser.

Originally published by  prince mudda at

Selenium Python

Today, companies and applications are widely structured on web-based systems. The information that these systems contain is dense and requires extensive processing. Various tasks are repeated, tedious and take significant amounts of time and money. These repetitive tasks can be taken care of by Web Automation. The common tasks in web automation include form filling, screen scraping, data collection, transferring between applications, website testing, and generating periodic reports.

Web Automation Tool

There are many tools available for automation. A variety of skill levels are required for the automation tool. A non-programmer might simply have to record some test scripts; whereas programmers and advanced testers need more advanced libraries and scripts.

Web browser automation tools work by logging the number of steps involved in the transaction and then playing that number back in the target web pages by injecting JavaScript and then tracking the results. The macro-like web automation tools are much more flexible and sophisticated.

One of the most popular web automation testing tools is Selenium. It was originally developed at Thought Works in 2004 by Jason Huggins as an internal tool. Selenium supports automation in various popular browsers, languages, and platforms.

It can be easily used on platforms such as Windows, Linux, Solaris, and Macintosh. It also supports OS for mobile applications, such as iOS, mobile Windows, and Android.

Selenium supports different programming languages using drivers specific to each language. Selenium-supported languages include C #, Java, Perl, PHP, Python, and Ruby. Test scripts in Selenium can be encoded using any supported languages and can run directly in almost all modern web browsers.

Let us take a look at the main advantages of this automation tool before going to the deeper sessions of Selenium.

  • Creates quicker reports.
  • Allows frequent testing of regressions.
  • Supports Agile.
  • Countless iterations can be done without impasse.
  • Easier documentation.
  • Errors in manual testing can be easily detected.
Initial Setup

We’ll have to do a couple of things to set up before we start. For functional tests to be written using Selenium Web Driver, a request must be sent to the Selenium server; test cases are then executed on different browsers. In our case, we’ll be working with Google Chrome. The very first step is to get chromedriver.exe to simulate the browser. The next step is to install the selenium package using pip. If your virtual environment is already there then simply type in the shell command line: pip install selenium

Now, we need to import the Selenium web driver to implement Python with Selenium. Before proceeding, we would like to understand more about Selenium Web Driver. It is a web-based automation testing framework that can test Web pages that have been initiated on different web browsers and operating systems.

Selenium WebDriver Client Library for Python allows us to use all the features of Selenium WebDriver and interact with Selenium Standalone Server in order to perform automated testing of both remote and distributed browser-based apps.

To import and configure dependencies to add libraries and functionalities, use the commands below to import Selenium Webdriver:

  • from selenium import webdriver
  • from selenium.webdriver.common.keys import keys
  • from selenium.import.*
Running Your First Selenium WebDriver Automation Script 

Let’s create a Python script with WebDriver, which uses Selenium classes and functions to automate browser interaction.

Here is a simple script to activate the browser:

driver = webdriver.Chrome()
assert "Google" in driver.title
element = driver.find_element_by_name("q")
assert "No results found." not in driver.page_source

Running the above code will create an instance of Chrome WebDriver. The driver.get method navigates to the page address provided by the URL. The page is loaded fully before WebDriver returns control to the script. However, WebDriver may not know if the page is loaded completely if the page uses a lot of AJAX on load.

The next line asserts that the title contains the word “Google”:

assert “Google” in driver.title

The next statement tries to locate the input text by its name attribute using the "find_element_by_name" method.

element = driver.find_element_by_name("q")

Now, we send keys. It’s similar to using your keyboard to enter keys. Use the "Keys" class imported from "selenium.webdriver.common.keys" to send special keys.


You will get the results if there is any once the submission of the page is done. Assertion can be made to make sure that results are found:

assert “No results found.” not in driver.page_source

The final step closes the window. In this script, the close() method is called, which will close one tab only. However, if only one tab was open, the browser will exit by default:

Locating Elements

Once the page is loaded, Selenium interacts with various elements on the page. There are a number of ways by which WebDriver finds elements using one of the "find_element_by_*" methods. We can use the most suitable method for our case.

Locating Element by Id

You can use the "findelementby_id" method to locate element by its id:

element = driver.find_element_by_id('element_id')

Locating Element by Name

To locate an element by name, you can use "findelementby_name" method:

element = driver.find_element_by_name('element_name')

Locating Element by XPath

If there is no appropriate id or name attribute for the item you want to locate, then you can use XPath. It can be used either to locate the element in absolute terms or relative to an element that has an id or name attribute. For example, let’s consider a contact form:

<form id="contactForm">
<input name="name" type="text" />
<input name="email" type="email" />
<input name="phone" type="tel" />
<input name="continue" type="submit" />

To locate the form elements, you can use XPath like this:

contact_form = driver.find_element_by_xpath("/html/body/form[1]")
contact_form = driver.find_element_by_xpath("//form[1]")
contact_form = driver.find_element_by_xpath("//form[@id='contactForm']")

Locating Hyperlinks by Link Text/partial Link Text

To locate a hyperlink, you can use "findelementby_link_text" or "find_element_by_partial_link_text." For example:

<a href="contact.html">Contact Us</a>
contact = driver.find_element_by_link_text('Contact Us')
contact = driver.find_element_by_partial_link_text('Cont')

Locating Elements by Tag Name

To locate an element by a tag, you can use "findelementby_tag." For example: <p>Lorem ipsum dolor</p>

element = driver.find_element_by_tag_name('p')

Locating Elements by Class Name

To locate an element by class name, you can use "findelementby_class_name." For example: <h1 class="heading">This is a heading</h1>

element = driver.find_element_by_class_name('heading')

Locating Elements by CSS Selectors

This method can be used when you need to locate an element by CSS selector syntax. For example: <p class="para"> Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>

paragraph = driver.find_element_by_css_selector('p.para')

These are all the methods by which we can locate elements in a browser. Let’s write a test case using Selenium.

Writing a Test Case With Selenium

Selenium is primarily used to write test cases. However, there is no testing tool/framework provided by the Selenium package. We can use Python’s unittest module to write test cases. For our example, we will create an example unittest script and save it as

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class GoogleSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def test_search_in_google_com(self):
driver = self.driver
self.assertIn("Google", driver.title)
element = driver.find_element_by_name("g")
assert "No results found." not in driver.page_source
def tearDown(self):
if __name__ == "__main__":

First, we import the unit test module, which is a built-in Python module based on Java’s Junit. Then, we create a class called GoogleSearch, where the test case class is inherited from "unittest.TestCase."

class GoogleSearch(unittest.TestCase):

The "setUp" is part of the initialization process; this method will be called before each test function you write in this test case class. An instance of Chrome WebDriver is then created.

def setUp(self):
self.driver = webdriver.Chrome()

In the test case method, the first line creates a local reference to the driver object created in the "setUp" method.

def test_search_in_google_com(self):
driver = self.driver

After every test method, the "tearDown" method will be called. This is a place where all the cleanup actions can be done. The browser window is closed in the current method.

def tearDown(self):

The final lines can be used to run the test suite:

if __name__ == "__main__":

Selenium is one of the widely used Web Browser Automation tools, as it offers plenty of functionality and control over all major web browsers. Although it is mainly used as a testing/automation tool for production or integration environment, but it can also be used as a web scraper

Originally published by  prince mudda at


Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

Complete Python Bootcamp: Go from zero to hero in Python 3

Building A Concurrent Web Scraper With Python and Selenium

JavaScript Testing using Selenium WebDriver, Mocha and NodeJS

Perform Actions Using JavaScript in Python Selenium WebDriver