1. Write XPath using the text() function
- 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()
- 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?
- child
- parent
- ancestor
- descendant
- following
- self
- following-sibling — same parent after the current node
- preceding
- preceding-sibling — same parent before the current node
syntax: /axis::tagName
3.What does dot(.) represent in xpath?
- 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?
- An immediate child in XPath is indicated using “/”.
- To find paragraph elements that are descendants of any div element.
- 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?
- Ancestor: Select all parents, grandparents, etc… of the current node
- 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
- child: Select all children of the current node
- 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
- following-sibling: All the siblings after the current node
- 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?
- Descendent: Select all children, grandchildren, etc… of the current node.
- Attribute: Select all attributes of the current node
21. Why do we use * (Astrik) in XPath?
- It is called a wildcard
- It gives them an element which matches the current location
- //div/*: This selects all children of the current node
- //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’)])
- Select any element in the HTML document that contains text data
- Text content which has substring as ‘data’
24. Why XPath selectors are important?
- Can be used to identify elements when there is a complex structure
- Can be traversed within the dom in both ways (up and down)
- Can be used to identify dynamically changing elements
- 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