Friday 7 May 2021

Different type of locators in webdriver selenium

 Webdriver locators

ID

This can be used when you know the id attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"



from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_id("username").send_keys("Roshni")


Name

This can be used when you know the name attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_id("username").send_keys("Roshni")
driver.find_element_by_name("pw").send_keys("Password")



Xpath

This can be used when you know the xpath attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"

//tagname[@attribute='value']

from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_id("username").send_keys("Roshni")
driver.find_element_by_name("pw").send_keys("Password")
driver.find_element_by_xpath("//input[@id='Login']").click()

We can also write regex for xpath as below

*[contains(@attribute,'value')]

Let us see one example for the same


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_id("username").send_keys("Roshni")
driver.find_element_by_xpath("//*[contains(@class,'password')]").send_keys("Roshni")
driver.find_element_by_xpath("//input[@id='Login']").click()

CSS

This can be used when you know the CSS attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"

//tagname[@attribute='value']

input[id='password']

from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_id("username").send_keys("Roshni")
driver.find_element_by_css_selector("input[id='password']").send_keys("Roshni"))
driver.find_element_by_xpath("//input[@id='Login']").click()

We can also write regex for css as below

[attribute*,'value']

Let us see one example for the same


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)
driver.find_element_by_id("username").send_keys("Roshni")
driver.find_element_by_css_selector("[class*='password']").send_keys("Roshni")
driver.find_element_by_xpath("//input[@id='Login']").click()

Generating CSS from ID

tagname#ID - tagname is again optional here

eg. input#username

Only used when you know id attribute or classname. For classname you have to put "."


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_css_selector("input#username").send_keys("Roshni")
driver.find_element_by_xpath("//*[contains(@class,'password')]").send_keys("Roshni")
driver.find_element_by_xpath("//input[@id='Login']").click()

CSS method of finding elements on a webpage is most fastest way to find elements. So I would recommend to use css method than to use any other while writing your code for selenium.

Classname

This can be used when you know the classname attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"


from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.get("https://login.salesforce.com/?locale=in")
driver.maximize_window()
print("Page Title : " + driver.title)
print("Current URL : " + driver.current_url)

driver.find_element_by_css_selector("input#username").send_keys("Roshni")
driver.find_element_by_class_name("password").send_keys("roshni")
driver.find_element_by_xpath("//input[@id='Login']").click()

Generating xpath based on text

//tagname[text()='aaa']

driver.find_element_by_xpath("//label[text()='Password']")

linktext

This can be used when you know the linktext attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"

driver.find_element_by_link_text("Forgot Your Password?").click()





No comments:

Post a Comment

Different type of locators in webdriver selenium

 Webdriver locators ID This can be used when you know the id attribute of an element. If the element is not found by mentioned attribute, it...