Did you know? Research improves critical thinking skills.

DIFFICULT / TRICKY XPath Interview Questions

Advanced XPath Interview Questions

Difficult XPath Interview Questions & Answers

Conceptual Tricky Questions

1. Difference between / and // in XPath?

Single slash (/) selects direct child nodes, while double slash (//) selects nodes at any level in the DOM.

2. Difference between absolute and relative XPath?

Absolute XPath starts from root (html/body), while relative XPath starts from any node using //.

3. Difference between findElement and findElements in XPath result?

findElement returns first matching element, while findElements returns list of all matching elements.

4. What is the use of dot (.) and double dot (..)?

Dot (.) refers to current node, and double dot (..) refers to parent node.

5. Difference between text() and . in XPath?

text() fetches only visible text node, while dot (.) fetches all child text including hidden or nested nodes.

Advanced XPath Functions

6. How to locate element using contains()?

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

7. How to locate element using starts-with()?

//button[starts-with(@class,'btn-')]

8. How to handle dynamic text using normalize-space()?

//span[normalize-space()='Login']

9. XPath to find element having multiple attributes?

//input[@type='text' and @name='username']

10. XPath using OR condition?

//button[@id='login' or @name='loginBtn']

Axes – Very Important (Interview Favorite)

11. What are XPath axes?

Axes are used to navigate DOM relative to current node like parent, child, sibling, ancestor, descendant.

12. XPath to locate parent element?

//input[@id='email']/parent::div

13. XPath to locate grandparent?

//input[@id='email']/ancestor::form

14. XPath to locate following sibling?

//label[text()='Username']/following-sibling::input

15. XPath to locate preceding sibling?

//input[@id='pwd']/preceding-sibling::label

Index & Position Based XPath

16. XPath to select 2nd element from list?

(//input[@type='text'])[2]

17. XPath to select last element?

(//div[@class='item'])[last()]

18. XPath to select second last element?

(//li)[last()-1]

19. Difference between [1] and (//tag)[1]?

[1] selects first child of each node, while (//tag)[1] selects first matching element in entire DOM.

Complex Real-Time XPath Scenarios

20. XPath for element based on text and attribute?

//button[text()='Submit' and @type='submit']

21. XPath for element inside table row having specific text?

//tr[td='John']//input[@type='checkbox']

22. XPath to locate element when ID changes dynamically?

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

23. XPath to find element without attribute value?

//input[not(@disabled)]

24. XPath to select element having child?

//div[span='Login']

25. XPath for element with exact and partial match?

//a[text()='Home']
//a[contains(text(),'Home')]

Very Tricky Interview Questions

26. Can XPath locate hidden elements?

Yes, XPath can locate hidden elements but Selenium cannot interact unless visible.

27. Which is faster: XPath or CSS?

CSS Selector is faster than XPath in most browsers.

28. Can XPath handle SVG elements?

Yes, using name() function: //*[name()='svg']

29. XPath to find element using contains(text())?

//p[contains(text(),'Welcome')]

30. When should XPath not be used?

When ID or Name is available, XPath should be avoided due to performance and fragility.



Source: sureshtechlabs.com


Share this post:

WhatsApp Facebook Twitter Telegram