locators in selenium

    - There are 8 locators.
    - id, class, name, linktext, partialLinktext,
      cssSelector, xpath, tagName
      
 - First I would check for id. If there is id,
  and it is not dynamic, I would go for id.
- If not, I would quickly check for it if 
  there is unique class or name attribute value
-If it is link, I would use linkText or
  partiallinktext
-If none available than I would go for xpath     

4
2
Awgiedawgie 440220 points

                                    - First I would check for id. If there is id,
  and it is not dynamic, I would go for id.
- If not, I would quickly check for it if 
  there is unique class or name attribute value
-If it is link, I would use linkText or
  partiallinktext
-If none available than I would go for xpath  

4 (2 Votes)
0
4.44
10
Phoenix Logan 186120 points

                                    LOCATORS
id: ID attribute value is unique to that specific webElement.
driver.findElement(By.id("id attribute's value")); 
Dynamic attribute value -> Refresh the page and 
check if the id attribute value is changing or not. 
name: looks for the value of the name attribute 
and returns the matching web element- it is not always unique.
driver.findElement(By.name("h131"));
className: looks for the value of class attribute. 
driver.findElement(By.className("h461"));
tagName: locates the webElement by tagName itself. 
-returns the first matching tagName 
driver.findElement(By.tagName("a"))  
linkText: locates the webElement by the text of the link
driver.findElement(By.linkText("Click here to go Google"));
partialLinkText: Only checks if the given text is contained in the link. 
driver.findElement(By.partialLinkText("TEXT"));  
cssSelector: It has its own syntax. 
It helps us create custom locators to locate webElements 
using different attributes and values. 
-> We are not just stuck using: id, name, class, linkText. 
-> We can use any attribute and their values to locate.
tagName[attribute='value'] / tagName.value 
==> looks for tagName with given class attribute value
How do we go from child to parent using cssSelector? 
-> You can NOT go to parent from child using cssSelector.
8- Xpath:
1- Absolute Xpath: - It starts with / "single slash" -> 
When we say /, it will go to the direct child of the given webElement
- Absolute xpath starts from root element 
- It is not recommended to use absolute xpath
- Because it is very fragile. Easily breaks when there is 
any minimal structural changes on HTML code.
/html/body/div/div/a --> This would return line C.    
2- Relative xpath: -> Starts with // "double slash” 
When we say // it allows us to start from anywhere in the html code.
-> When we say //, it will jump and return all of the 
given webElements inside of the HTML 
Commonly used relative xpath syntaxes:
1- //tagName[@attribute='value']  2- //*[@attribute='value']  
3- //tagname[.='text'] 
If we are using attribute value with xpath, and it is returning 
us more than 1 option.
We need to surround the whole xpath with paranthesis. 
ex: (//button[@class='btn btn-primary'])[2]

4.44 (9 Votes)
0
3.86
7
Lionel Aguero 33605 points

                                    --> WHAT ARE LOCATORS? 
    - Locators are methods that are used to locate HTML web elements for 
      Selenium browser driver.
      
--> WHAT IT IS USED FOR? 
    - It is used to locate any specific web element 
    on the UI of AUT (application under test)

--> HOW MANY Selenium LOCATORS?
   - There are 8 locators.
    - id, class, name, linktext, partialLinktext, cssSelector, xpath, tagName

3.86 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
selenium locators by value use locator with selenium what are selenium locators different types of locators in selenium importance of locators in selenium what's the other name for locators in selenium can be? custom selenium locators selenium locator types of selenium locators selenium locators example where we keep locators in selenium what are the different types of locators in selenium? which one is preferable to use? all locators in selenium with examples different locators in selenium with examples locators selenium guru99 what is locator in selenium locators in selenium guru99 locators selenium selenium 4 locators different locators in selenium locator in selenium advanced selenium locate elements is a an input on for element locator python selenium locators list selenium locators list locate element and assign it as a number locating gui elements in selenium how to find locator in selenium selenium type locator selenium locattors selenium locator by attribute locator types in selenium locator strategies element locators in selenium locators syntaxin selenium test a locator for selenium selenium css take locator if presetn how to javascript in selenium locators All HTML elements must have which of the following attributes class tag none of these attributes are required locator id name If using the HTML name attribute to locate an element, Selenium returns a list of all elements with the specified name que son los locators selenium data cy will be selenium locator selenium locator by css using and locator element locating all() javascript selenium how to find locator for enter in selenium how to find locator by id locator by id selenium locators which locator to choose how to choose locator in selenium types of locators in selenium what are locators in selenium locators in selenium
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source