XPath _relative_给HTMLUnit / Groovy中的给定元素? [英] XPath _relative_ to given element in HTMLUnit/Groovy?

查看:222
本文介绍了XPath _relative_给HTMLUnit / Groovy中的给定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想评估一个XPath表达式相对于给定的
元素。



我一直在这里阅读:
http://www.w3schools.com/xpath/default.asp



看起来下面的语法之一应该可以工作(尤其是没有领先的
斜线或后代:)

然而,在HTMLUnit中似乎没有任何工作。任何帮助非常感谢(哦
这是一个groovy脚本顺便说一句)。谢谢!



http://htmlunit.sourceforge.net /



http://groovy.codehaus .org /



Misha






 #!/ usr / bin / env groovy 

import com.gargoylesoftware.htmlunit.WebClient

def html =
< head>< head>< title> Test< / title>< / head>
< body>
< div class ='levelone'>
< ; div class ='leveltwo'>
< div class ='levelthree'/>
< / div>
< div class ='leveltwo'>
< div class ='levelthree'/>
< div class ='levelthree'/>
< / div>
< / div>

< / body>
< / html>


def f = new File('/ tmp / test.html')
if(f.exists()){
f.delete()
}
def fos = new FileOutputStr eam(f)
fos<<< html
> b $ b&def webClient =新的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'])//这个
给出命名空间错误
assert element.size()== 0

谢谢!!!

解决方案

从问题的定义中可以看出,评估XPath表达式的相关元素是什么。假设这是文档节点,那么以下XPath表达式将选择所需的节点:

  * / * / div [@ class ='levelone'] 

html / body / div [@ class ='levelone']

descendant :: div [@ class ='levelone']

如果在实际的XML文档(未显示)中存在默认名称空间,则可能有问题。在这种情况下,您需要使用您的XPath托管语言(我不知道groovy)来定义/注册这个名称空间,并使用关联的前缀,如下所示:

  * / * / x:div [@ class ='levelone'] 

x:html / x:body / x:div [@ class ='levelone']

descendant :: x:div [@ class ='levelone']


I would like to evaluate an XPath expression relative to a given element.

I have been reading here: http://www.w3schools.com/xpath/default.asp

And it seems like one of the syntaxes below should work (esp no leading slash or descendant:)

However, none seem to work in HTMLUnit. Any help much appreciated (oh this is a groovy script btw). Thank you!

http://htmlunit.sourceforge.net/

http://groovy.codehaus.org/

Misha


#!/usr/bin/env groovy

import com.gargoylesoftware.htmlunit.WebClient

def html="""
<html><head><title>Test</title></head>
<body>
<div class='levelone'>
 <div class='leveltwo'>
    <div class='levelthree' />
 </div>
 <div class='leveltwo'>
    <div class='levelthree' />
    <div class='levelthree' />
 </div>
</div>

</body>
</html>
"""

def f=new File('/tmp/test.html')
if (f.exists()) {
 f.delete()
}
def fos=new FileOutputStream(f)
fos<<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

Thank you!!!

解决方案

It is not clear from the definition of the problem, what is the element relative to which the XPath expressions are evaluated. Assuming that this is the document node, then the following XPath expressions will select the desired node:

   */*/div[@class='levelone']

   html/body/div[@class='levelone']

   descendant::div[@class='levelone']

You may have problem if in the actual XML document (not shown), there is a default namespace. In this case you need to define / register this namespace in your XPath-hosting language (I don't know groovy) and use the associated prefix, like this:

   */*/x:div[@class='levelone']

   x:html/x:body/x:div[@class='levelone']

   descendant::x:div[@class='levelone']

这篇关于XPath _relative_给HTMLUnit / Groovy中的给定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆