Xpath

 1. Write XPath using the text() function

  1. If the specified element does not contain any attributes and if it contains text then we can identify that element by using xpath by text()
  2. It is applicable for both absolute and relative path
tagName[text()='textValue']
//td[text()='Java']
text() can be represented by using dot(.)
//td[.='Java']
 Attribute values and the text values are case and space sensitive.
//div[text()='Login ']

2. What are the different types of axes which used to travel in XPath?

  1. child
  2. parent
  3. ancestor
  4. descendant
  5. following
  6. self
  7. following-sibling — same parent after the current node
  8. preceding
  9. preceding-sibling — same parent before the current node

syntax: /axis::tagName

3.What does dot(.) represent in xpath?

  1. current node
".//*div," the dot refers to the current node,
and "//*div" is selecting all div elements at any level
beneath the current node.
//The dot (.) at the beginning means the context is the current node.
//The "//*" part selects any descendant node, regardless of its type.
//Finally, "div" selects only those nodes that are div elements.

4. How do you write an xpath locator to identify paragraph elements that are the immediate child of a div element or the descendent of a div element?

  1. An immediate child in XPath is indicated using “/”. 
  2. To find paragraph elements that are descendants of any div element. 
  3. We can use “//” in the XPath.

5.Write XPath using the contains keyword

//*div[contains(@class,'column')]

6. Write XPath using the starts-with keyword

//*[starts-with(@id, 'row')]
//*[starts-with(@class,'audio')]

7. Write XPath using the text() function

//p[starts-with(text(),'Content')]

8. Write XPath using the following keyword

//div[@id='row']//following::p

9. Write XPath using the ancestor keyword

//div[@id='content']//ancestor::div

10. What does it mean by Following and Ancestor keywords?

  1. Ancestor: Select all parents, grandparents, etc… of the current node
  2. following: Select everything after the current node

11. Write XPath using the child keyword

//div[@id='row']//child::div

12. Write XPath using the preceding keyword

//div[@class='row']//preceding::div

13. Explain the Child and Preceding keywords

  1. child: Select all children of the current node
  2. preceding: Select all nodes that appear before the current node except parent and grandparent(ancestors)

14. Write XPath using the following-sibling keyword

//div[@class='row']//following-sibling::div

15. Write XPath using the parent keyword

//div[@class='row']//parent::div

16. Explain following-sibling and parent keywords

  1. following-sibling: All the siblings after the current node
  2. parent: parent node of the current node

17. Write XPath using self keyword

//div[@class='row']//self::div

18. Write the XPath using the Descendant keyword

//div[@id='row']//descendant::div

19. Write XPath using the attribute keyword

//div[@id='row']//attribute::class

20. What are the Descendant and Attribute keywords?

  1. Descendent: Select all children, grandchildren, etc… of the current node.
  2. Attribute: Select all attributes of the current node

21. Why do we use * (Astrik) in XPath?

  1. It is called a wildcard
  2. It gives them an element which matches the current location
  3. //div/*: This selects all children of the current node
  4. //div//* this selects all descendants of the current node

22. How to identify paragraph elements that are immediate children of the dev element or descendent of the dev element?

//div[@class=’row’]//child::p
//div[@class='row']//descendant::p

23. Explain this “ xpath(//*[contains(text(), ‘data’)])

  1. Select any element in the HTML document that contains text data
  2. Text content which has substring as ‘data’

24. Why XPath selectors are important?

  1. Can be used to identify elements when there is a complex structure
  2. Can be traversed within the dom in both ways (up and down)
  3. Can be used to identify dynamically changing elements
  4. Possible to identify elements using their text, position, parent, child, and sibling relationships

25. What is the difference between absolute xpath and relative xpath

absolute xpath : Use / (single slash) to start from the root node in the dom. Can traverse only down.

relative xpath: Use // (double slash) start from anywhere which matches the given mechanism. Can traverse from up and down both.

26. Write an XPath using the class attribute

//div[@class=’example’]

27. Write an Xpath using the id attribute

//div[@id=’content’]

28. Write an Xpath using combining two classes.

//div[@class=’row’]//div[@class=’example’]
//div[(@class='row') or (@class='sound')]

No comments:

Post a Comment

Cucumber

What is Cucumber? Definition : Cucumber is an open-source tool that supports BDD . Purpose : Bridges the communication gap between business ...