无法找到Chrome DevTools提供的XPath [英] XPath given by Chrome DevTools could not be found

查看:153
本文介绍了无法找到Chrome DevTools提供的XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在回答关于Selenium的另一个问题,我在Chrome上发现了XPath的奇怪行为(我没有尝试过其他浏览器)。



http://www.amcharts.com/svg- maps /?map = usa ,为什么不能 XPath工作?



// * [@ id =chartdiv] / div / div [1] / svg / g [7] / g / g [1]

这是确实



// * [@ id =chartdiv] / div / div [ 1] / * [名称()= SVG] / * [名称()= G] [7] / * [名称()= 克] / * [名称()= G] [ 2]






我必须进行以下更改:


  • / g 变成 / * [name()=g]

  • / svg 变成 / * [name()= svg]

  • / path 变成 / * [name()= path]



奇怪的是,我通过右键单击元素来获得第一个XPath HTML并复制XPath。

这里>






我在 http://gmsgroup.com/municenter.asp 其中:


  • // * [@ id中= 'EXT-gen203'] / * [名称()= 'SVG'] / * [名称()= 'A'] / * [名称()= '路径'] [ (@d,'M136.65')] / .. works,

  • // * [@ id = 'ext-gen203'] / * [name()='svg'] / * [name()='a'] [@ title ='Hawaii'] 不会。 >


这里, / * [name()='a'] [@ title ='Hawaii'] 无法获得< a title =Hawaii>< / a> ,为什么?

解决方案命名空间可以是前缀:

 < root xmlns:ns =www.example.com /> 

或默认名称空间:

 < root xmlns =www.example.com/> 

SVG位于默认名称空间中。像 / svg 这样的路径表达式告诉XPath处理器寻找一个名为svg的元素,并且这个元素没有任何名称空间。因此,此表达式仅匹配:

 < svg />或< svg xmlns =/> 

使用 / * [name()='svg'] 只会排除一个前缀名称空间。它仍然允许一个名为svg的元素,该元素位于默认名称空间中,不带前缀。第二个表达式匹配

 < svg xmlns =http://www.w3.org/2000/svg/> ; 

但不是

 < ns:svg xmlns:ns =http://www.w3.org/2000/svg/> 






为了证明这一点,请考虑以下XSLT样式表:

 <?xml version =1.0encoding =UTF-8?> 

< xsl:template match =* [name()='svg']>
< hit />
< / xsl:template>

< / xsl:transform>

应用于以下输入,一个缩短的SVG文档:

 <?xml version =1.0encoding =UTF-8?> 
< svg xmlns =http://www.w3.org/2000/svg/>

结果:

 <?xml version =1.0encoding =UTF-8?> 
< hit />

请在线查看这里






编辑(回应您的评论)
$ b


更清楚的是,这是否意味着SVG默认名称空间会覆盖页面中的名称空间,因此不会处理[ title ='Hawaii']?

HTML元素本身没有名称空间 - 而SVG不会覆盖页面的名称空间。 svg 元素上的默认名称空间仅适用于其自身,并且所有子元素(包括 a [@ title ='Hawaii'] $ b

// * [@ id ='ext-gen203'] / * [name()='svg '] / * [name()='a'] [@ title ='Hawaii'] 应该能够找到 a 元素和


在哪里可以找到该默认命名空间的规范?

blockquote>

如果您的意思是默认名称空间,请看这里: http://www.w3.org/TR/REC-xml-names/#scoping-default 。如果您的意思是SVG规范,请参阅SVG规范的以下章节: http://www.w3.org/TR/SVG/struct.html


这是否也意味着没有更好的获取方式// [@ ID = 'EXT-gen203'] / [名称()= 'SVG'] / [名称()= 'A'] / [名称( )='path'] [starts-wit h(@d,'M136.65')] / ..?


更好的方法是通过 register 声明这些名称空间,从而通过前缀使它们可用于Xpath表达式中(如Martin Honnen指出的那样)。 Chrome开发工具不是我的专业领域,我不能评论如何在那里做。


As I was experimenting with some XPath SVG paths while answering another question about Selenium, I found out a strange behaviour with XPath on Chrome (I didn't try on some other browsers).

On http://www.amcharts.com/svg-maps/?map=usa, why doesn't that XPath work?

//*[@id="chartdiv"]/div/div[1]/svg/g[7]/g/g[1]

Whereas this one does?

//*[@id="chartdiv"]/div/div[1]/*[name()="svg"]/*[name()="g"][7]/*[name()="g"]/*[name()="g"][2]


I had to make the following changes:

  • /g becomes /*[name()="g"]
  • /svg becomes /*[name()="svg"]
  • /path becomes /*[name()="path"]

The weird thing is that I got the first XPath by right-clicking the element HTML and copied the XPath from there.


I have another example on http://gmsgroup.com/municenter.asp where:

  • //*[@id='ext-gen203']/*[name()='svg']/*[name()='a']/*[name()='path'][starts-with(@d, 'M136.65')]/.. works,
  • //*[@id='ext-gen203']/*[name()='svg']/*[name()='a'][@title='Hawaii'] doesn't.

Here, /*[name()='a'][@title='Hawaii'] couldn't get <a title="Hawaii"></a>, why?

解决方案

Namespaces can either be prefixed:

<root xmlns:ns="www.example.com"/>

or default namespaces:

<root xmlns="www.example.com"/>

SVG is in a default namespace. A path expression like /svg tells an XPath processor to look for an element that is called "svg" and that is in no namespace whatsoever. Therefore, this expression only matches:

<svg/>   or <svg xmlns=""/>

Using /*[name()='svg'] does only rule out a prefixed namespace. It still allows for an element that is called "svg" and that is in a default namespace, without a prefix. This second expression matches

<svg xmlns="http://www.w3.org/2000/svg"/>

but not

<ns:svg xmlns:ns="http://www.w3.org/2000/svg"/>


To prove this, consider the following XSLT stylesheet:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:template match="*[name() ='svg']">
        <hit/>
    </xsl:template>

</xsl:transform>

Applied to the following input, a shortened SVG document:

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"/>

Results in:

<?xml version="1.0" encoding="UTF-8"?>
<hit/>

See for yourself online here.


EDIT (responding to your comment)

More clearly, does it mean that the SVG default namespace overrides the one from the page and therefore doesn't handle expressions such as [@title='Hawaii']?

The HTML elements themselves do not have a namespace - and SVG does not "override" the page's namespace. The default namespace on the svg element only applies to itself and all child elements (including the a[@title='Hawaii'] element inside it.

//*[@id='ext-gen203']/*[name()='svg']/*[name()='a'][@title='Hawaii']should be able to find the a element and I am very surprised it does not.

Where to find the specification for that default namespace?

If you mean default namespaces in general, look here: http://www.w3.org/TR/REC-xml-names/#scoping-defaulting. If you meant the SVG specification, study the following chapter of the SVG specification: http://www.w3.org/TR/SVG/struct.html.

Does it also mean that there isn't a better way of getting than //[@id='ext-gen203']/[name()='svg']/[name()='a']/[name()='path'][starts-wit‌​h(@d, 'M136.65')]/..?

The better way to do this is to register or declare those namespaces and thereby make them available for use in Xpath expressions by means of prefixes (as Martin Honnen has pointed out). Chrome Dev Tools is not my field of expertise, I cannot comment on how to do it there.

这篇关于无法找到Chrome DevTools提供的XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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