

Starts to locate elements after the given parent node. I think you got the idea if not, please write a comment and I will clarify your questions. In the below example,ġ- First, it finds the element whose XPath is Then, starts to find all div elements in the node where the above XPath belongs to.Įxample : can select specific div groups by changing div depths as shown – Returns 53 Returns 33 – Returns 24 Returns 19 nodes 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.
How to write xpath for text in selenium 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
