Note that there are some explanatory texts on larger screens.

plurals
  1. POXPath _relative_ to given element in HTMLUnit/Groovy?
    primarykey
    data
    text
    <p>I would like to evaluate an XPath expression <em>relative</em> to a given element.</p> <p>I have been reading here: <a href="http://www.w3schools.com/xpath/default.asp" rel="nofollow noreferrer">http://www.w3schools.com/xpath/default.asp</a></p> <p>And it seems like one of the syntaxes below should work (esp no leading slash or descendant:)</p> <p>However, none seem to work in HTMLUnit. Any help much appreciated (oh this is a groovy script btw). Thank you!</p> <p><a href="http://htmlunit.sourceforge.net/" rel="nofollow noreferrer">http://htmlunit.sourceforge.net/</a></p> <p><a href="http://groovy.codehaus.org/" rel="nofollow noreferrer">http://groovy.codehaus.org/</a></p> <p>Misha</p> <hr> <pre><code>#!/usr/bin/env groovy import com.gargoylesoftware.htmlunit.WebClient def html=""" &lt;html&gt;&lt;head&gt;&lt;title&gt;Test&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;div class='levelone'&gt; &lt;div class='leveltwo'&gt; &lt;div class='levelthree' /&gt; &lt;/div&gt; &lt;div class='leveltwo'&gt; &lt;div class='levelthree' /&gt; &lt;div class='levelthree' /&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; """ def f=new File('/tmp/test.html') if (f.exists()) { f.delete() } def fos=new FileOutputStream(f) fos&lt;&lt;html def webClient=new WebClient() def page=webClient.getPage('file:///tmp/test.html') def element=page.getByXPath("//div[@class='levelone']") assert element.size()==1 element=page.getByXPath("div[@class='levelone']") assert element.size()==0 element=page.getByXPath("/div[@class='levelone']") assert element.size()==0 element=page.getByXPath("descendant:div[@class='levelone']") // this gives namespace error assert element.size()==0 </code></pre> <p>Thank you!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload