Selenium Python – Understanding WebDriver, WebElement
In this chapter, we will introduce the entities WebDriver, WebElement, and By of the Selenium module. The WebDriver refers to the browser object. The WebElement refers to the HTML elements on the page. The By helps us create the locator objects on the page. So let us understand how are they used, and what all methods are associated with them.
Structure
- WebDriver
- WebElement
- By
Objective
The Selenium module of Python programming language contains three crucial entities—WebDriver, WebElement, and By. The WebDriver helps us automate the browser object and has methods for it. The WebElement helps us automate the HTML element on the page, and has methods to perform an action on them. The By class helps us locate objects using different locator strategies supported by Selenium, like ID, name, XPATH, and others.
Introduction to Selenium module
The Selenium module in Python contains three important entities, WebDriver, WebElement, and By. In the following table, it shows us the relation of these entities with our objects of automation
The following table shows their relation to the web entity:
WebDriver | Browser |
WebElement | HTML element |
By | Locator to identify the element on the page |
Let us understand these entities in more detail.
WebDriver
The WebDriver object refers to the browser. It controls the browser by sending commands to the browser via the JSONWireProtocol. The details of this protocol are available here: https://github.com/Se- leniumHQ/Selenium/wiki/JsonWireProtocol. Any implementation of WebDriver for automating any browser will have to abide by this. Let us have a look at the few methods available which help in browser actions:
Methods | Description |
get(url) | Opens a web page with the given URL. |
findElement(By) | This method takes a By object as an argument and finds the object on the web page which matches that locator. It returns the first found match. In Python we have find_element_by_id, find_ element_by_name,                                                   find_element_by_xpath methods to locate elements. |
findElements(By) | This method returns a list of all elements which match the locator value provided by the By object on the web page. |
Forward( ) | This command will take you to the next page. |
back() | This command will take you to the previous page. |
page_source | This command will return the entire HTML content of the page. |
Title | Returns the current title of the page. |
current_url | Returns the current URL of the page. |
Close | Close the current instance of the browser. |
Quit | Close the current instance of the browser and all the associated windows. |
WebElement
The WebElement object refers to the HTML element on the web page. All methods that interact with the DOM will work through this interface. Before the implementation of any method, it does a freshness check to ensure if the element is still valid, and only then it acts. If the element reference is not valid it throws StaleElementReferenceException. Let us have a look at some of the methods available with this interface:
Methods | Actions |
click() | It performs click operation on a web element. |
clear() | It performs a clear operation on a web element. |
get_attribute() | This method returns the data associated with the property of the HTML element at the time of execution. For example, you can fetch the href attribute with an anchor element. |
is_displayed() | Returns true, if the element is visible to the user. |
is_enabled() | Returns true, if the element is enabled. |
is_selected() | Returns true, if the element is selected, for example, a radio button is selected. |
send_keys() | It allows typing of text on an element, generally a textbox. |
Text | It fetches the text associated with the HTML element. |
By
The By class of Selenium allows us to locate the web element on the page. It uses the following strategies:
- ID
- NAME
- XPATH
- CSS_SELECTOR
- TAG_NAME
- LINK_TEXT
- PARTIAL_LINK_TEXT
- CLASS_NAME
To locate the element on a web page we use the find_element_by_* method, where the * replaces any of the preceding locator strategies to find the element on the web page.
Now let us try to write down the script for the scenario of login-logout using the methods we have learned above for these entities. The steps for the login-logout for our application are as follows:
- Open the application with URL: http://practice.bpbonline. com/catalog/index.php
- Click on the My Account link.
- Type email address and password.
- Click the Sign In button.
- Click the Log Off link.
- Click the Continue link.
We have learned in earlier chapters that a link is to be identified by its link text which we see on the screen. To identify the username, we can use the name property having the value email_address, the password field has the name property password, and the sign-in button has an id property table, as you can see in the HTML of its page:
<table border="0" cellspacings="0" cellpadding="2" width="1001"> <tr> <td class="fieldkey"> E-mail Address:</td> <td class="fieldvalue"><input type="text" name="email_address"/></td> </tr> <tr> <td class="fieldkey">password:</td> <td class="fieldvalue"><input type="password" name="password" maxlenght="40" /></td> </tr> </table> <p><a href="http://practice.bpbonline.com/catalog/password_forgotten.php">password forgotten? click here.</a></p> <p align="right"><span class="tablink"><button id="tab1" type="sbmit">sign Inc/button></span><script>
Let us now write the steps to automate the above scenario.
So in the following code, we are clicking on the MyAccount link, then typing the email address and password. Then we click on the Sign In button, and if our credentials are valid we should be able to login into the application. Then we click on the Log Off link and click on the Continue link to complete the process.
from selenium import webdriver browser = webdriver.chrome(executble_path='D:\Ecl.ipse\BPB\seleniumpython\seleniumpython\drivers\chromedriver.exe') browser.get('http://practice.bpbonline.com/catalog/index.php') browser.find_element_by_link_text("My Account").click() browser.find_element_by_name("email_address").send_keys("[email protected]") browser.find_element_by_name("password").send_keys("bpb@123") browser.find_element_by_id("tab1").click( ) browser.find_element_by_link_text("tab off").click( ) browser.find_element_by_link_text("continue").click( ) browser.quit( )
Conclusion
In this chapter, we learned how Selenium WebDriver helps automate the browser, identify the element on a web page and perform actions on it. The above test script can be executed on Firefox and Internet Explorer browsers by changing the executable path to their respective WebDrivers. The rest of the test script looks exactly the same. We understood the different methods associated with the WebDriver object, WebElement object, and the By locator object, which allows objects to be identified using different locator strategies.
In the next chapter, we will see how we can structure our Selenium scripts using PyUnit tests. We will study the unittest framework, which has derived its features from the JUnit and NUnit framework.
Related Articles:
- Chapter 1 Introduction to Selenium
- Chapter 2 Selenium IDE
- Chapter 3 Locators in Selenium
- Chapter 4 Installation and setup
- Chapter 6 Unittest in Python
- Chapter 7 Synchronizing Test
- Chapter 8 Concept of Parameterization
- Chapter 9 Working with Different Web Elements
- Chapter 10 Frames, Alerts, and Action Class
- Chapter 11 Page Object Model
- Chapter 12 Selenium-Grid