![how to write xpath for text how to write xpath for text](https://i.stack.imgur.com/UTDKz.jpg)
As you see in the below screenshot I started with “//div”, then I bypassed h1 and strong tags, and continue to my search with the second “//” and then find the element in the span tag. We can also use trailing “//” to continue our XPath search.
#How to write xpath for text how to
In the next section, you will also learn how to find an element with text by XPath in Selenium. In the above screenshot, SelectorsHub add-on finds a bit different result like: //spanbut I used also my tactic which is finding Xpath with text: //span Starts with a double slash “//” which means it can start to search anywhere in the DOM structure.Įxample: //span.Starts from the middle of the HTML DOM.Starts with a single slash “/” which means starting to search from the root.Įxample: /html/body/div/div/main/div/section/div/div/div/div/div/div/div/h1/strong/span.It is a direct way to locate an element.Generally, in some test automation engineer interviews, I asked the difference between absolute and relative XPath. You can also use class and src attributes to locate an element with XPath as shown above but always check that there is only one element has been found! Absolute XPath and Relative XPath in Selenium The basic syntax for XPath is shown below: We can find the location of any element on a web page using XML path expressions. In the below sections, I will share with you 15 Tactics to Write Effective XPath Locators. These locators must be capable to locate complex and dynamically changing web elements. In these kinds of situations, we need to use dynamic XPath locators. However, sometimes we could not find any of them in the DOM, and also sometimes locators of some elements change dynamically in the DOM. In our test automation projects, we generally prefer to use id, name, class, etc.
![how to write xpath for text how to write xpath for text](https://i.stack.imgur.com/uTSqW.png)
How to Write Dynamic XPath in Selenium Projects for Web Elements