Name Locator


Locating elements by name are very similar to locating by ID, we are using Name Property instead of ID.
Name=Name of the element
Scenario :
Enter User Name and Password (using Name property)
Click on the Login button.
Close the browser

Following is the steps to locate the element using Name property on webpage.

    Right click on Email or Phone webelement on the page and select inspect option in the context menu



Pick the name property value of the Email or Phone webelement


 Syntax for locating a web element using its Name attribute is written as:

driver.find_element(By.NAME,<Name Value>)
or
driver.find_element_by_name(<Name Value>)

same way you have to follow for other elements (Password and Login button).

Below is the Script
from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath=
"C:\\BrowserDrivers\\chromedriver.exe"

driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.facebook.com/")

driver.find_element(By.NAME,
"email").send_keys("Hello")
driver.find_element_by_name(
"pass").send_keys("hello")
driver.find_element(By.ID,
"loginbutton").click()

Note: If there are multiple elements with the same Name locator then the first element on the page is selected. Test may fail, if another element with the same Name locator is present on the web page.


Comments