Identifying or locating the element on the web or mobile is known as locator. We can access the element by using below methods along with the locator types such as id , className , name , css selector , tagName , linkText , partiallinkText, XPath etc.
In selenium we have totally 8 types of locators which are used for identify the elements on the webpage.Below are the types of locators in selenium.
There are two types of XPaths 1.) Relative Xpath , 2.) Absolute Xpath.
We will discuss one by one later in this section.
Suppose we need to enter a text in the edit box manually. Users can enter it by using the keyboard.
But in Test Automation first we need to access the element using its locator value and we can find it on a webpage by using find_element() method by passing its locator type and locator value.
WebDriver class has two types of methods such as find_element() and find_elements() that helps us to identify the elements on a webpage by using above locator types and perform the required operation on it.
1. First we need to create the object for the webdriver class by calling along with the appropriate browser driver method and pass the browser driver path to it.
driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/drivers/chromedriver.exe")
2. Now by using a webdriver object we can access the methods such as find_element() and find_elements().
driver.find_element()
3. We need to pass the locator type and locator value in those required methods.
driver.find_element(By.ID,"user_input")
4. Now finally, we can do the action on those required webelement.In below line we are sending the text on the edit box by using send_keys() method.
driver.find_element(By.ID,"user_input").send_keys("Skill2Lead")
a. In two ways we can perform action on the webelement.
Directly we can assign the action at the end of the find_element() itself.
driver.find_element(By.ID,"user_input").send_keys("Skill2Lead")
b. By creating a variable and assigning the action to it.
ele = driver.find_element(By.ID,"user_input") ele.send_keys("Skill2Lead")
This method is used to identify groups of WebElements which are assigned to the same locator.
For Example, We have a menu item in the webpage and we need to validate all the values of the menu. Now by using find_elements() method we will access the menu by its locator values. Now , By using for loop we will iterate and then print all the values in a console to verify.
Syntax: ele = driver.find_elements(By.ID,"menu")
This method returns output as a list value and by using for loop we can iterate all the values and perform the required action.
ele = driver.find_elements(By.ID,"menu") for menuItem in ele: print(menuItem.text)