{"id":71408,"date":"2020-01-10T11:28:48","date_gmt":"2020-01-10T05:58:48","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=71408"},"modified":"2024-04-12T14:13:41","modified_gmt":"2024-04-12T08:43:41","slug":"beware-the-xpath-trap","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/","title":{"rendered":"Beware the XPath \/\/ trap"},"content":{"rendered":"<h1><strong>Beware the XPath \/\/ trap<\/strong><\/h1>\n<p><a ref=\"magnificPopup\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-78036 size-full\" src=\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png\" alt=\"Beware the XPath \/\/ trap\" width=\"620\" height=\"295\" srcset=\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png 620w, https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG-300x143.png 300w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>Let&#8217;s understand Beware the XPath \/\/ trap.\u00a0In XPath the expression \/\/ means something very specific, and it might not be what you think. Contrary to common belief, \/\/ means &#8220;anywhere in the document&#8221; not &#8220;anywhere in the current context&#8221;. As an example:<\/p>\n<p>page.find(:xpath, &#8216;\/\/body&#8217;).all(:xpath, &#8216;\/\/script&#8217;)<\/p>\n<p>You might expect this to find all script tags in the body, but actually, it finds all script tags in the entire document, not only those in the body! What you&#8217;re looking for is the .\/\/ expression which means &#8220;any descendant of the current node&#8221;:<\/p>\n<p>page.find(:xpath, &#8216;\/\/body&#8217;).all(:xpath, &#8216;.\/\/script&#8217;)<\/p>\n<p>The same thing goes for within:<\/p>\n<p>within(:xpath, &#8216;\/\/body&#8217;) do<\/p>\n<p>page.find(:xpath, &#8216;.\/\/script&#8217;)<\/p>\n<p>within(:xpath, &#8216;.\/\/table\/tbody&#8217;) do<\/p>\n<p>&#8230;<\/p>\n<p>end<\/p>\n<p>end<\/p>\n<p>Web Form<\/p>\n<p>Capybara has user-friendly API for it as<\/p>\n<p>&lt;form id=&#8221;myform&#8221;&gt;<\/p>\n<p>&lt;input type=&#8221;text&#8221; name=&#8221;Forename&#8221; value=&#8221;&#8221; \/&gt;<\/p>\n<p>&lt;input type=&#8221;text&#8221; name=&#8221;Surname&#8221; value=&#8221;&#8221; \/&gt;<\/p>\n<p>&lt;input type=&#8221;submit&#8221; value=&#8221;Go&#8221; \/&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>The following Cucumber step definition would fill in and submit the form<\/p>\n<p>When(\/^I complete and submit the form$\/) do<\/p>\n<p>fill_in &#8216;Forename&#8217;, :with =&gt; &#8216;Matthew&#8217;<\/p>\n<p>fill_in &#8216;Surname&#8217;, :with =&gt; &#8216;Robbins&#8217;<\/p>\n<p>click_on &#8216;Go&#8217;<\/p>\n<p>end<\/p>\n<p>The above example clearly explains working with windows. We hope this is section is now clear to you. But, in case you need more assistance, we&#8217;re always here to help. So, try the links below for further assistance.<\/p>\n<p><strong>Make your resume stand out and become a Certified Capybara Testing Professional. <a href=\"https:\/\/www.vskills.in\/practice\/index.php?route=test\/search&amp;search=capybara\">Try free practice tests here!<\/a><\/strong><\/p>\n<p><strong>A great career is just a certification away. So, practice and validate your skills to become a <a href=\"https:\/\/www.vskills.in\/certification\/testing\/capybara-testing-certification\">Certified Capybara Testing Professional.<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Beware the XPath \/\/ trap Let&#8217;s understand Beware the XPath \/\/ trap.\u00a0In XPath the expression \/\/ means something very specific, and it might not be what you think. Contrary to common belief, \/\/ means &#8220;anywhere in the document&#8221; not &#8220;anywhere in the current context&#8221;. As an example: page.find(:xpath, &#8216;\/\/body&#8217;).all(:xpath, &#8216;\/\/script&#8217;) You might expect this to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8248],"tags":[8289],"class_list":["post-71408","page","type-page","status-publish","hentry","category-capybara","tag-beware-the-xpath-trap"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Beware the XPath \/\/ trap - Vskills Tutorial<\/title>\n<meta name=\"description\" content=\"Enhance and Upgrade your Beware the XPath \/\/ trap skills for better career opportunities. Become a Certified Capybara Testing Professional Now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Beware the XPath \/\/ trap - Vskills Tutorial\" \/>\n<meta property=\"og:description\" content=\"Enhance and Upgrade your Beware the XPath \/\/ trap skills for better career opportunities. Become a Certified Capybara Testing Professional Now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:43:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/\",\"name\":\"Beware the XPath \/\/ trap - Vskills Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png\",\"datePublished\":\"2020-01-10T05:58:48+00:00\",\"dateModified\":\"2024-04-12T08:43:41+00:00\",\"description\":\"Enhance and Upgrade your Beware the XPath \/\/ trap skills for better career opportunities. Become a Certified Capybara Testing Professional Now!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/#primaryimage\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Beware the XPath \/\/ trap\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Beware the XPath \/\/ trap - Vskills Tutorial","description":"Enhance and Upgrade your Beware the XPath \/\/ trap skills for better career opportunities. Become a Certified Capybara Testing Professional Now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/","og_locale":"en_US","og_type":"article","og_title":"Beware the XPath \/\/ trap - Vskills Tutorial","og_description":"Enhance and Upgrade your Beware the XPath \/\/ trap skills for better career opportunities. Become a Certified Capybara Testing Professional Now!","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:43:41+00:00","og_image":[{"url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/","name":"Beware the XPath \/\/ trap - Vskills Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/#primaryimage"},"thumbnailUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png","datePublished":"2020-01-10T05:58:48+00:00","dateModified":"2024-04-12T08:43:41+00:00","description":"Enhance and Upgrade your Beware the XPath \/\/ trap skills for better career opportunities. Become a Certified Capybara Testing Professional Now!","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/#primaryimage","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2020\/01\/DFG.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/beware-the-xpath-trap\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Beware the XPath \/\/ trap"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=71408"}],"version-history":[{"count":5,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71408\/revisions"}],"predecessor-version":[{"id":78037,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/71408\/revisions\/78037"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=71408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=71408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=71408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}