SELENIUM BASICS:


SELENIUM WEBDRIVER:


TESTNG:


REPORTS:


BUILD & CONTINUOUS INTEGRATION TOOLS:


CROSS BROWSER TESTING:


MOBILE TESTING:


AUTOMATION FRAMEWORKS:


API Testing:


INTERVIEW QUESTIONS:


SAMPLE RESUME:

Locators in Selenium Python

In this tutorial, we will learn Locators in Selenium and how to find it when you are writing scripts using Python.

Also, we discussed locators in Selenium when using Java

What are the locators available in Selenium?

Types of locators used in Selenium Python are as follows

  1. ID
  2. Name
  3. Class Name
  4. Tag Name
  5. Link Text
  6. Partial Link Text
  7. CSS Selector
  8. XPath

Let’s discuss these Selenium WebDriver locators in detail.

#1. Name

The name attribute in the HTML code is used as a locator for identifying elements. There may be multiple elements having the same value of name attribute in that case the first matching element will be identified. NoSuchElementException is thrown if there is no matching element with the name we are looking for.

Syntax: find_element_by_name (“software”)

Click this link for a detailed explanation of Name Locator

#2. Id

The id attribute in the HTML code is used as a locator for identifying elements. The ids are generally unique for an element. NoSuchElementException is thrown if there is no matching element with the id we are looking for.

Syntax: find_element_by_id (“test”)

Click this link for a detailed explanation of ID Locator

#3. Class name

The class name attribute in the HTML code is used as a locator for identifying elements. There may be multiple elements having the same value of class name attribute in that case the first matching element will be identified. NoSuchElementException is thrown if there is no matching element with the class name we are looking for.

Syntax: find_element_by_class_name (“test-auto”)

Click this link for a detailed explanation of Class Name Locator

#4. Tag Name

The Tag Name in the HTML code is used as a locator for identifying elements. There may be multiple elements having the same Tag Name in that case the first matching element will be identified. NoSuchElementException is thrown if there is no matching element with the Tag Name we are looking for. Tag Name locator in Selenium is generally useful for getting the content enclosed in the tag.

Syntax: find_element_by_tag_name (“input”)

Click this link for a detailed explanation of Tag Name Locator

#5. Link Text

The text enclosed within an anchor tag is used to identify a link or hyperlink. There may be multiple elements having the same link text, in that case, the first matching element will be identified. NoSuchElementException is thrown if there is no matching element with the link text we are looking for.

Syntax: find_element_by_link_text (“Selenium Python”)

Click this link for a detailed explanation of Link Text Locator

#6. Partial Link Text

The partial text enclosed within an anchor tag is used to identify a link or hyperlink. . There may be multiple elements having the same partial link text, in that case, the first matching element will be identified. NoSuchElementException is thrown if there is no matching element with the partial link text we are looking for.

Syntax: find_element_by_partial_link_text (“Selenium”)

Click this link for a detailed explanation of Partial Link Text Locator

#7. CSS Locator in Selenium

The element is identified with the CSS created with the help of HTML attribute, value, or tagName. NoSuchElementException is thrown if there is no matching element with the CSS we are looking for. A customized CSS expression can be built by following some of the below rules:

a) With the help of id and Tag Name. First, we need to specify the Tag Name followed by (#) then the id. Also, Tag Name is optional and can be omitted from the CSS expression.

Syntax: find_element_by_css_selector (“input#txt”)

b) With the help of class name and Tag Name. First, we need to specify the Tag Name followed by (.) then the class name. Also, Tag Name is optional and can be omitted from the CSS expression.

Syntax: find_element_by_css_selector (“input.txt-bx”)

c) With the help of Tag Name and any attribute with its values. First, we need to specify the Tag Name followed by the attribute with its value enclosed in ([]).

Syntax: find_element_by_css_selector (“a [alt=’title’]”)

d) To identify a sibling element, we need to specify the nth-child (n) of the parent in a CSS expression. Suppose to navigate to the second column in a table, for that we have to use the nth-child concept.

Syntax: find_element_by_css_selector (“tr td: nth-child (2)”)

Click this link for a detailed explanation of CSS Selector Locator

#8. XPath

The element is identified with the XPath created with the help of HTML attribute, value, and tagName. NoSuchElementException is thrown if there is no matching element with the XPath we are looking for. Xpath is of two types absolute and relative. For absolute XPath, we have to traverse from root to the element. However, for relative XPath, we can start from any position in DOM. An XPath expression should follow a particular rule- // tagname [@attribute=’value’]. The Tag Name is optional. If it is omitted, the expression should //*[@attribute=’value’].

Syntax: find_element_by_xpath (“//a [@alt=’title’]”)

Click this link for a detailed explanation of XPath Locator

Sharing is caring.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Get our latest blog posts delivered to your inbox

Subscribe and get popular blog posts about software testing industry.

Rajkumar

Leave a Comment