Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy htmlunit getByXPath_Xpath_Groovy_Href_Htmlunit - Fatal编程技术网

Groovy htmlunit getByXPath

Groovy htmlunit getByXPath,xpath,groovy,href,htmlunit,Xpath,Groovy,Href,Htmlunit,我目前正在使用HtmlUnit试图从页面中获取href,但遇到了一些问题 XPath是: /html/body/div[2]/div/div/table/tbody/tr/td[2]/div/div[5]/div/div[2]/span/a 在网页上,它看起来像: <a class="t" title="This Brush" href=http://domain.com/this/that">Brush Set</a> 然而,这将返回那里的所有内容,而不仅仅

我目前正在使用HtmlUnit试图从页面中获取href,但遇到了一些问题

XPath是:

/html/body/div[2]/div/div/table/tbody/tr/td[2]/div/div[5]/div/div[2]/span/a    
在网页上,它看起来像:

<a class="t" title="This Brush" href=http://domain.com/this/that">Brush Set</a>
然而,这将返回那里的所有内容,而不仅仅是我想要的url


有人能解释一下我必须添加什么才能获得href吗?(也不是以.html结尾)

您正在选择
a
。您想选择
a/@href

hrefs = page.getByXPath("//html/body/div[2]/div/div/table/tbody/tr/td[2]/div/div[5]/div/div[2]/span/a[@class='t']/@href")

您正在选择
a
。您想选择
a/@href

hrefs = page.getByXPath("//html/body/div[2]/div/div/table/tbody/tr/td[2]/div/div[5]/div/div[2]/span/a[@class='t']/@href")

谢谢你的及时回复。您知道为什么会出现以下内容以及url吗DomAttr[name=href value=我对Groovy不太熟悉,但我猜这是因为您选择了属性并获得了对象的“toString()”表示,而不是它的字符串值。请尝试使用
hrefs.getValue()
你说得对,Mads Hansen。非常感谢。作为可能遇到类似问题的任何人的旁白,我不得不使用page.getFirstByXPath而不是page.getByXPath。如果你有时间,请检查:谢谢你的及时回复。你知道为什么会出现以下内容以及url吗?:DomAttr[name=href value=我对Groovy不太熟悉,但我猜这是因为您选择了属性并获得了对象的“toString()”表示形式,而不是字符串值。请尝试使用
hrefs.getValue()
你说得对,Mads Hansen。非常感谢。对于可能遇到类似问题的任何人,我不得不使用page.getFirstByXPath而不是page.getByXPath。如果你有时间,请检查: