Table of content

Xpath Interview Questions

  1. What is Normalize space in Xpath?
    Normalize space is used to remove the empty spaces at the ends of the string while using xpath.
    normalize-space(string)
    
    str = "   cher      cher     tech     "
    normalize-space(string) // outputs "cher cher tech"​

    With code example

    html code
    <label>    who cares         about spaces   </label>
    
    xpath
    //label[normalize-space(text())='who cares about spaces']
  2. How do you handle text in XPath which is dynamic?
    Using the text function, we can find the elements using the text present in them.
    <button type="button">Blueberry</button>    (Blueberry is text here)
    
    xpath with text : //button[text()='Blueberry']​

    But when the text is dynamic we have to use contains function along with text function. For this to work some part of the string must be static.

    <button type="button">*****berry</button>   
    
    xpath with text : //button[contains(text(),'berry')]​

  3. How do you handle lower and upper case attributes in Xpath?
    We can use @ method for an attribute, but if the attribute values change every time lower to upper case or mix case value when the page refreshes, in this @ method may not help us.

    During such kinds of situations, we must ignore the case(UPPER/lower).
    Syntax :// tagname[text(), 'sourceCaseContent', 'targetCaseContent'), 'value']​
  4. How do you execute Xpath in Jquery?
    We can execute the xpath in jQuery using $x
    $x("//xpath")
  5. Why Xpath is slower compared with?

    XPath and CSS work almost with the same speed; even on this website, we have performed the tests couple of times but there is no considerable difference in speed.

    So when the interviewer asks this kind of a question you should be knowing.

  6. What are Axes in Xpath?
    XPath axes are used to identify elements that periodically change or refresh their attributes by their relationship like a parent, child, sibling, based on the independent element, whose properties do not change.

  7. When the position function is used?
    Position function helps the user to get the match at a particular index, using position we can get elements that are less than the position or greater than the position as well.

  8. Can you use Xpath inside a frame?
    The frame is nothing but another web page but showcased a different web page. So once you go into the frame you can use the XPath as if it is a normal web page.
  9. How do you find parent elements without using Axes in Xpath?
    In UNIX we use two dots to navigate to parent similar to that in xpath we can use two dots to navigate to the parent
    //input/../​
  10. Write XPath for a Button?
    Here, some people ask without giving any instruction for the questions. You have to ask the question of whether you should consider the button source code on your own.

    You should know to create a button; otherwise, the intention of the question will not be satisfied. You can form the button in two ways, either using the button tag or input tag.

    Xpath for button
    // form button using input tag
    <input type='button' value='ABCD'>
    Xpath : //input[@type='button']
    
    // form button using the button tag
    <button>XYZ</button>
    Xpath : //button​

  11. Can you write xpath for aria labels?
    You can write xpath for any attribute present in the HTML tag; aria-label has been used to help visually impaired people. We can write xpath to aria labels as if it is an ID or a class.
<button aria-label="Close" onclick="myDialog.close()">X</button>

Xpath :  //button[@aria-label='Close']
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions