In the below expression, match the starting text of the attribute used for finding the element whose attribute changes dynamically. Starts-with function is used to find the element whose value of the attribute changes on refresh or any operation on the webpage. XPath(Parent node): = ‘text’]//parent::*. It will then select the parent node of the input tag having Id = ‘text’. XPath of the Parent node: = ‘text’]//parent::span. We will then find the XPath of the parent element node of the current node using parent syntax, as shown below screenshot. So, we will first find the XPath of the current node. Let us choose a search input box as a current node. Then we will find the parent of the current node. Open the website and right-click on the search box.
![xpath for text contains xpath for text contains](https://i0.wp.com/www.protechskills.com/wp-content/uploads/2014/08/21.png)
The syntax of the parents axis is given below: For all the other element nodes, the parent axis contains maximum one node. Therefore, when the current node is the root node, the parent axis is empty.
![xpath for text contains xpath for text contains](http://makeseleniumeasy.com/wp-content/uploads/2018/09/UnevenWhitespaces.jpg)
The parent node will be either the root node or an element node. The parent axis will select the parent of the current node. It fails to find the element if any one condition is and the below expression, we highlight the ‘First Name’ element as it has both attributes ‘id’ and ‘name.’ Both conditions should be true for finding the element. In the below XPath, it identifies the elements whose single or both conditions are or both elements as ‘First Name’ element having attribute ‘id’ and ‘Last Name’ element having attribute ‘name.’ĪND expression uses two conditions. This means that any one condition should be true to find the element. It is also applicable if any one of the condition is true, or maybe both. OR expression uses two conditions, whether the first condition OR second condition should be true. List elements = driver.findElements(By.xpath(“//h1/parent::form//h2”)) Below is the syntax to use ‘contains’ function within the XPath.
Xpath for text contains code#
The syntax for locating elements through XPath- Using contains() method can be written XPath- contains() method, we can write the Java code along with the dynamic XPath location the ‘contains’ function, we can extract a list of web elements containing the matching text throughout the web page.
![xpath for text contains xpath for text contains](https://i.stack.imgur.com/bJaVj.png)
Let us take a scenario in which we will try locating the Google Search Engine Text box by XPath- Using contains() method.įollow the steps given below for locating the textbox provided on the home page of Google Search Engine. XPath also allows you to select individual elements, attributes, and some other parts of XML documents for specifying the location of a particular web element.Ĭontains() is used to identify an element when we are familiar with some part of the value of an element’s attribute. XPath can also be used as a substitute when you do not have a suitable id/name attribute for the locator element. XPath is a language used to locate nodes in XML documents.