Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Locators in Selenium WebDriver

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 231
    Comment on it

    Locators in Selenium Webdriver:

    In Selenium WebDriver, everything is related to web elements. All the objects in a web page is called web elements like input box, buttons, etc. To do any action with these web elements, we need to first locate these elements. So to locate these web elements, locators are used. There are 7 types of locators.


    Types of locators:-

    1) Id: This is the most preferred way to locate the elements on a web page because ID is unique for each web element.

    For Example1:

    <input id="ID1" class="required" type="text" name="Demo">
    

    In the above example, this web element(i.e., input box) can be located by its ID i.e., "ID1".

    Syntax of using locators: WebElement we= driver.findElement(By.id("ID1"));


    2) Name: Elements are also located by their field name.
    In the above example(Example 1), web element(input box) can be located by its name i.e., "Demo". Syntax: WebElement we1=driver.findElement(By.name("Demo"));


    3) LinkText: This is used when you know the link within the anchor tag.
    For Example3: <a href="www.w3schools.com"> Schools Name </a>
    In the above example, web element (i.e., Schools Name link) can be located by its link name "Schools Name"

    Syntax: WebElement we2=driver.findElement(By.linkText("Schools Name"));


    4) PartialLinkText: This is used when you are locating the element via partial link name.
    In the above example(Example3), web element (i.e., Schools Name link) can be located by its partial link name i.e., either by "Schools" or by "Name"

    Syntax: WebElement we2=driver.findElement(By.partialLinkText("Schools"));


    5) TagName: Used to locate element by tag name.
    In the above example(Example3), web element (i.e., Schools Name link) can also be located by its tag name i.e., "a"

    Syntax: WebElement we2=driver.findElement(By.tagName("a"));


    6) className: Locate element by the name of the className.
    In the above example(Example1), web element (i.e., input box) can also be located by its class name i.e., "required" Syntax: WebElement we2=driver.findElement(By.className("required"));


    7) Xpath: It is also a type of the locator. Xpath is itself a vast topic. So i'll cover this in my next blog.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: