It finds the element before the ancestor statement and sets it as a top node and then starts to find the elements in that node. We can find an element with its exact text. “and” is case-sensitive, you should not use the capital “AND”. “or” is case-sensitive, you should not use a capital “OR”. In this method, we use two interrogation conditions such as A and B and return a result-set as shown below: A We chained “//div” with “ //span’ in the below example. We can chain multiple relative XPath declarations with “//” double slash to find an element location as shown below. It is very handy to use when the attribute value changes dynamically but also you can use this method for non-changing attribute values. This method checks the starting text of an attribute. * -> It searches the text "here" in the -> It searches "" link in the DOM. When an attribute of an element is dynamic, then you can use contains() for the constant part of the web element but also you can use contains() in any condition when you need.Įxample: -> It searches "btnClk" for all name attributes in the DOM. It is a very handy XPath Selenium locator and sometimes it saves the life of a test automation engineer. Writing Smart XPaths for Complex and Dynamic Elements Tag – Attribute – Value TrioĮxample = // alt=’ Onur -> "*" means, search "swtestacademy" class for all Contains XPath in Selenium Finally, the statement is finalized as shown below.Įxample: //div//spanīy the way, I used the SelectorsHub add-on but also you can use ChroPath or Ranorex Selocity as well. 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.
#Long path tool cnet dow 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 Dynamic XPath in Selenium Projects for Web Elements Next Article: Selenium CSS Selectors (Complete Reference).Locate an Element inside Array of Elements.