ID Locator

ID’s are unique for each element so it is easy way to locate elements using ID Locator.ID locators are the fastest and safest locators out of all locators.

Format:
Id=id of the webElement

Scenario :
Enter UserName and Password  (using ID property )
Click on the Login button.(using ID Property)
Close the browser

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

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


Pick the Id value(email) of Email or Phone webelement.


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

driver.find_element(By.ID,<ID Value>)
or
driver.find_element_by_id(<ID 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.ID,
"email").send_keys("Hello")
driver.find_element_by_id(
"pass").send_keys("hello")
driver.find_element(By.ID,
"loginbutton").click()

Note: If you are using By.ID then you have to import below package
from selenium.webdriver.common.by import By

Comments