• Home

  • Courses

    • Run Java Selenium Tests in Azure Devops
    • Interviewing for Testing Positions
  • For Business

  • Trainings

    • Beginner Selenium (May 2018)
    • Advanced Selenium (Feb 2019)
  • Teacher

  • Bootcamp Info

  • Forum

  • Blog

  • Meetups

    • Test Automation for Responsive Site
    • All about Locators in Selenium Projects
  • More..

    • FAQ
    • Cheat Sheets
    • Presentations
  • More...

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    alexsiminiuc3
    May 16, 2018
    Edited: May 16, 2018

    XPATH examples

    in Selenium Questions

    For the login page of the VPL.ca site (https://vpl.bibliocommons.com/user/login?destination=%2Fuser_dashboard),

    the username, pin and login elements can be found not only by name but also by xpath.


    This is the HTML code of each element:


    USERNAME

    <input class="field_username text" 
           data-js="username_login" 
           id="user_name_2o8quogf7rlg8keurmdf7ra09p" 
           name="name" 
           testid="field_username" 
           type="text">


    PIN

    <input autocomplete="on" 
           class="text" 
           id="user_pin_2o8quogf7rlg8keurmdf7ra09p" 
           name="user_pin" 
           testid="field_userpin" 
           type="password" 
           value="">


    LOGIN

    <input alt="Log In" 
           class="btn btn-primary" 
           data-js="button_login" 
           name="commit" 
           testid="button_login" 
           title="Log In" 
           type="submit" 
           value="Log In">


    All elements have a testid attribute with static values so possible xpath expressions are


    USERNAME

    //input[@testid = 'field_username']

    PIN

    //input[@testid = 'field_userpin']

    LOGIN

    //input[@testid = 'button_login']


    Additional expressions can be created for each element:


    USERNAME

    //input[@class = 'field_username text']
    //input[@data-js = 'username_login']
    //input[contains(@id, 'user_name')]


    PIN

    //input[contains(@id, 'user_pin')]


    LOGIN

    //input[@data-js = 'button_login']
    //input[@value = 'Log In']


    Any expression can be used if it identifies the element uniquely.


    But locating by name is better than using an xpath selector.


    8 comments
    0
    8 Comments

    Share Your ThoughtsSign up to leave a comment.

    alexsiminiuc3
    May 18, 2018

    lets say that you need to find an element.


    you dont know yet how to find it, by id or by name or by xpath.


    you look first to the html code of the element in chrome inspector.


    if the element has an id attribute with a static value, then you can locate the element by id.

    if the element has an name attribute with a static value, then you can locate the element by name.


    otherwise, you may have to create an xpath expression (or css) that finds the element.

    Like

    elena.musienko15
    May 18, 2018

    But how can I know that I need to write that expression (I mean $x("//input[@testid = 'field_username']") in console tab, if I don't know that xpath yet?

    Like

    alexsiminiuc3
    May 18, 2018

    if you write in the console $x("//input[@testid = 'field_username']") and you get a result after pressing enter, that means that the xpath matches 1 element.


    so you can use the xpath expression in the java code as a locator for finding that element:

    By usernameLocator = By.xpath("//input[@testid = 'field_username']");
    WebElement username = driver.findElement(usernameLocator);

    or


    WebDriverWait wait = new WebDriverWait(driver, 10);
    By usernameLocator = By.xpath("//input[@testid = 'field_username']");
    WebElement username = wait.until(ExpectedConditions
                              .elementToBeClickable(usernameLocator));

    Testing an xpath expression in the console tab of Chrome Inspector has 1 purpose only.


    To check that the xpath expression works by finding 1 element.



    Like

    elena.musienko15
    May 17, 2018

    Alex, can you please explain how to find xpath? I wrote in 'console' $x("//input[@testid = 'field_username']")

    but I actually don't understand what to do with the result. I also tried on another examples where we use it and I still don't get it.

    Like

    alexsiminiuc3
    May 16, 2018

    you are welcome.

    Like

    vikan29
    May 16, 2018

    Thanks Alex.

    Like

    alexsiminiuc3
    May 16, 2018

    On the login page, to get the username field through xpath, type the following in the Console tab of the Chrome Inspector:


    $x("//input[@testid = 'field_username']")

    Like

    vikan29
    May 16, 2018

    Alex, can you provide the full text what I should type at the Console for page https://vpl.bibliocommons.com/user/login?destination=%2Fsearch%3Fq%3Djava%26t%3Dsmart%26search_category%3Dkeyword

    to get the xpath and analyze the result. Thanks.

    Like
    8 comments

    © 2021 Vancouver Selenium WebDriver Automation Training