Html 使用 xpath 查找具有特定内容的跨度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10177169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:49:59  来源:igfitidea点击:

Finding a span with specific content using xpath

htmlxmlxpathselenium

提问by Joe Bienkowski

I have some span's like this:

我有一些这样的跨度:

<span> 
  <span>foobar</span>  
  <span>thisisatest</span> 
</span>

I'm trying to use xpath to find the span with "thisisatest" in it.

我正在尝试使用 xpath 来查找包含“thisisatest”的跨度。

I've tried this:

我试过这个:

span[text()='thisisatest']

span[text()='thisisatest']

and it doesn't seem to be working.

它似乎不起作用。

回答by Petr Jane?ek

You are missing //at the beginning of the XPath.

您缺少//XPath 的开头。

  • One slash would mean "a span that is a child of a root node".
  • Two slashes mean "find me any span with that text".
  • 斜线表示“作为根节点的子节点的跨度”。
  • 两个斜杠的意思是“找到我任何带有该文本的跨度”。

//span[text()='thisisatest']

//span[text()='thisisatest']

回答by axcdnt

You could try this:

你可以试试这个:

span[span= "thisisatest"] 

All <span>elements that contain at least one <span>element child with the value thisisatest.

<span>包含至少一个<span>值为 thisisatest 的子元素的所有元素。

Hope it helps!

希望能帮助到你!