This is our second article on Python Selenium, in this article we are going to learn about Web Elements Introduction in Python Selenium. in the first article we had a simple introduction to selenium and python selenium and also how you can add selenium web drivers for python programming language. you can check the link of the first article in the below.
Python Selenium Articles
1: Python Selenium Introduction & Installation
Web Elements
A web page is created of many different HTML elements, such as buttons, links, a body, labels, forms, and so on, that are named WebElements in the context of WebDriver. Together, these elements on a web page will achieve the business functionality. For example, let’s look at the HTML code of the login page of a website.
1 2 3 4 5 6 7 8 9 10 |
<html> <body> <form id="loginForm"> <input type="text" name="Username" placeholder="Please enter your username"/> <input type="password" name="Password" placeholder="Please enter your password"/> <input type="submit" /> </form> <a href="forgotPassword.html">Forgot Password ?</a> </body> </html> |
You can see in the above code that our login.html file is created by different web elements, for example we have text input fields for entering our password and username, also we have a button for submitting the form. and also you can see that for these web elements we have attributes, for example for input fields we have name attribute. UI Automation in selenium is mostly about locating these WebElements on a web page and executing user actions on them. and there are different ways that you can locate a web element in a web page, for example you can use id, name, tag, xpath, that we will use some of them in this article.
Locating Web Elements
OK now let’s start our example by automating Google Search page, which we want opening of the Google Search page, locating the search input field, typing something and searching for that.
1 2 3 4 5 6 7 8 9 10 11 12 |
from selenium import webdriver import time driver = webdriver.Chrome() driver.get('https://www.google.com') search_box = driver.find_element_by_name("q") search_box.send_keys("Python") #time.sleep(5) search_box.submit() |
So in the above example we are going to use the name locator of web elements, first of all we have imported the required classes, and after that we have opened our chrome browser and finding Google website.
Now in here we are locating the name attribute of input field web element like this.
1 |
search_box = driver.find_element_by_name("q") |
After that we are sending the python key to the Google Search input field, and at the end we are searching that.
1 2 |
search_box.send_keys("Python") search_box.submit() |
Also you can do the same functionality using the xpath like this. make sure that you have copied the xpath of the web element.
1 2 3 4 5 6 7 8 9 |
driver = webdriver.Chrome() driver.get('https://www.google.com') search_box = driver.find_element_by_xpath("//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input") search_box.send_keys("Python") #time.sleep(5) search_box.submit() |
So let’s create a little complex example, in this example we are going to use Mercury Tour website, it has a nice login form for testing web elements .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from selenium import webdriver driver = webdriver.Chrome() driver.get('http://newtours.demoaut.com/') user_element = driver.find_element_by_name("userName") pass_element = driver.find_element_by_name("password") btn_login = driver.find_element_by_name("login") user_element.send_keys("mercury") pass_element.send_keys("mercury") btn_login.click() |
So basically in this example, first of all we are going to open Mercury Tour website , there is a login form in the website, the password and username of this login form is mercury. first of all we find the locator of the two input fields with submit button, and after that we send the password and username to the input fields, and at the end we login.
Also you can watch the complete video for Python Selenium Web Elements Introduction
Subscribe and Get Free Video Courses & Articles in your Email