{"flag":true,"single":true,"pageTitle":"Xpath all commands tutorial","post":{"id":84,"user_id":"1","slug":"xpath-all-commands-tutorial-jfyo","title":"Xpath all commands tutorial","body":"<p><strong>\/ single slash is used for direct child, ie <\/strong>\/html\/body\/div\/header<\/p>\r\n<p><strong>\/\/ double is used for find anywhere ie<\/strong> \/\/header<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><strong>1. Selecting nodes<\/strong><br>Command: Selects all 'div' nodes in the document.<\/p>\r\n<pre class=\"language-markup\"><code>\/\/div<\/code><\/pre>\r\n<p><strong>2. Selecting nodes with specific attribute<\/strong><br>Command: \/\/[@attribute='value']&nbsp; \/\/Selects all 'a' nodes with the class attribute value 'example'.<\/p>\r\n<pre class=\"language-markup\"><code>&lt;a class=\"example\"&gt;hello &lt;\/a&gt;\r\n\/\/a[@class='example']\r\n\r\n&lt;div class=\"loepr\" data-id=\"review-badge-date\"&gt;hello &lt;\/div&gt;\r\n\/\/div[@data-id='review-badge-date']<\/code><\/pre>\r\n<p><strong>3. Selecting nodes with any attribute<\/strong><br>Command: \/\/nodename[@attribute] \/\/Selects all 'img' nodes with a 'src' attribute.<\/p>\r\n<pre class=\"language-markup\"><code>\/\/img[@src]<\/code><\/pre>\r\n<p><strong>4. Selecting nodes with specific text<\/strong><br>\/\/nodename[text()='text']&nbsp; \/\/Selects all 'p' nodes with the exact text 'Hello, world<\/p>\r\n<pre class=\"language-markup\"><code> \/\/p[text()='Hello, world!'] <\/code><\/pre>\r\n<p><strong>5. Direct child :<\/strong> Absolute path. only work if the document structure exactly matches that hierarchy.<\/p>\r\n<pre class=\"language-markup\"><code>\/html\/body\/div\/header\r\n\r\nGo to html&gt;body&gt;div&gt;header<\/code><\/pre>\r\n<p><strong>6.NthElement : <\/strong>Select Direct first child or second<\/p>\r\n<pre class=\"language-markup\"><code>\/html\/body\/div[1]\r\n\/html\/body\/div[2]<\/code><\/pre>\r\n<p><strong>7. Selecting text :: <\/strong>Text select of an element<\/p>\r\n<pre class=\"language-markup\"><code>\/html\/body\/div\/header[1]\/text()<\/code><\/pre>\r\n<p><br><br><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>Cheat sheet<\/p>\r\n<p>&nbsp;<\/p>\r\n<table style=\"width: 90.7839%; height: 541px;\" border=\"1\" cellspacing=\"0\" cellpadding=\"6\">\r\n<thead>\r\n<tr style=\"height: 44.7917px;\">\r\n<th style=\"width: 15.8014%; height: 44.7917px;\">Symbol \/ Expression<\/th>\r\n<th style=\"width: 24.2802%; height: 44.7917px;\">Meaning<\/th>\r\n<th style=\"width: 23.124%; height: 44.7917px;\">Example<\/th>\r\n<th style=\"width: 36.7094%; height: 44.7917px;\">Result<\/th>\r\n<\/tr>\r\n<\/thead>\r\n<tbody>\r\n<tr style=\"height: 44.7917px;\">\r\n<td style=\"width: 15.8014%; height: 44.7917px;\">\/<\/td>\r\n<td style=\"width: 24.2802%; height: 44.7917px;\">Absolute path (root &rarr; direct child)<\/td>\r\n<td style=\"width: 23.124%; height: 44.7917px;\">\/html\/body\/div<\/td>\r\n<td style=\"width: 36.7094%; height: 44.7917px;\">Selects the &lt;div&gt; that is a direct child of &lt;body&gt;<\/td>\r\n<\/tr>\r\n<tr style=\"height: 44.7917px;\">\r\n<td style=\"width: 15.8014%; height: 44.7917px;\">\/\/<\/td>\r\n<td style=\"width: 24.2802%; height: 44.7917px;\">Anywhere (descendant, any depth)<\/td>\r\n<td style=\"width: 23.124%; height: 44.7917px;\">\/\/div<\/td>\r\n<td style=\"width: 36.7094%; height: 44.7917px;\">Selects all &lt;div&gt; elements in the document<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"width: 15.8014%; height: 22.3958px;\">.<\/td>\r\n<td style=\"width: 24.2802%; height: 22.3958px;\">Current node (self)<\/td>\r\n<td style=\"width: 23.124%; height: 22.3958px;\">.\/\/p<\/td>\r\n<td style=\"width: 36.7094%; height: 22.3958px;\">From current node, select all &lt;p&gt; descendants<\/td>\r\n<\/tr>\r\n<tr style=\"height: 44.7917px;\">\r\n<td style=\"width: 15.8014%; height: 44.7917px;\">..<\/td>\r\n<td style=\"width: 24.2802%; height: 44.7917px;\">Parent of current node<\/td>\r\n<td style=\"width: 23.124%; height: 44.7917px;\">..\/h2<\/td>\r\n<td style=\"width: 36.7094%; height: 44.7917px;\">From current node, go up one level and select &lt;h2&gt;<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"width: 15.8014%; height: 22.3958px;\">@<\/td>\r\n<td style=\"width: 24.2802%; height: 22.3958px;\">Attribute selector<\/td>\r\n<td style=\"width: 23.124%; height: 22.3958px;\">\/\/a\/@href<\/td>\r\n<td style=\"width: 36.7094%; height: 22.3958px;\">Selects the href attribute values of all &lt;a&gt;<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"width: 15.8014%; height: 22.3958px;\">[@attr='value']<\/td>\r\n<td style=\"width: 24.2802%; height: 22.3958px;\">Filter by attribute value<\/td>\r\n<td style=\"width: 23.124%; height: 22.3958px;\">\/\/div[@id='main']<\/td>\r\n<td style=\"width: 36.7094%; height: 22.3958px;\">Selects &lt;div id=\"main\"&gt;<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"width: 15.8014%; height: 22.3958px;\">contains(@attr,'text')<\/td>\r\n<td style=\"width: 24.2802%; height: 22.3958px;\">Partial attribute match<\/td>\r\n<td style=\"width: 23.124%; height: 22.3958px;\">\/\/div[contains(@class,'header')]<\/td>\r\n<td style=\"width: 36.7094%; height: 22.3958px;\">Matches &lt;div class=\"site-header\"&gt;<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"width: 15.8014%; height: 22.3958px;\">[n]<\/td>\r\n<td style=\"width: 24.2802%; height: 22.3958px;\">Nth element (1-based)<\/td>\r\n<td style=\"width: 23.124%; height: 22.3958px;\">(\/\/p)[1]<\/td>\r\n<td style=\"width: 36.7094%; height: 22.3958px;\">Selects the first &lt;p&gt; in the document<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"width: 15.8014%; height: 22.3958px;\">text()<\/td>\r\n<td style=\"width: 24.2802%; height: 22.3958px;\">Node text<\/td>\r\n<td style=\"width: 23.124%; height: 22.3958px;\">\/\/h1\/text()<\/td>\r\n<td style=\"width: 36.7094%; height: 22.3958px;\">Extracts the text inside &lt;h1&gt;<\/td>\r\n<\/tr>\r\n<tr style=\"height: 22.3958px;\">\r\n<td style=\"width: 15.8014%; height: 22.3958px;\">*<\/td>\r\n<td style=\"width: 24.2802%; height: 22.3958px;\">Wildcard (any element)<\/td>\r\n<td style=\"width: 23.124%; height: 22.3958px;\">\/\/div\/*<\/td>\r\n<td style=\"width: 36.7094%; height: 22.3958px;\">All direct children of &lt;div&gt;<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>","category_id":"16","is_private":"0","created_at":"2023-03-29T12:49:04.000000Z","updated_at":"2025-09-02T22:35:38.000000Z","category":{"id":16,"user_id":"1","name":"Other","slug":"other-wpma","parent_id":null,"created_at":"2023-03-29T12:48:34.000000Z","updated_at":"2023-03-29T12:48:34.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"\/ single slash is used for direct child, ie \/html\/body\/div\/header \/\/ double is used for find anywhere ie \/\/header &nbsp; 1. Selecting nodesC - Xpath all commands tutorial (Updated: September 2, 2025) - Read more about Xpath all commands tutorial at my programming site [SITE]","categories":[]}